Skip to content

Commit

Permalink
fix: image tests
Browse files Browse the repository at this point in the history
- use local image instead of previous version
- fix docker localhost

Signed-off-by: vsamidurai <vilvaram@gmail.com>
  • Loading branch information
vsamidurai authored and vsamidurai committed Aug 26, 2023
1 parent 663aafb commit 905caf1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion base-image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test: build-image
$(IMAGE):$(TAG)

build-test-ci: build-image
go test -mod=readonly -v -count=1 --race ./...
TEST_IMAGE_NAME=$(IMAGE) TEST_IMAGE_TAG=$(TAG) go test -mod=readonly -v -count=1 --race ./...

list-gems:
@docker run -ti --rm \
Expand Down
63 changes: 48 additions & 15 deletions base-image/fluentd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ var counterTotal = 5
func TestFluentd(t *testing.T) {
assert := assert.New(t)
path, err := os.Getwd()
log.Println(path)
if err != nil {
log.Println(err)
}
ctx := context.Background()

imageName := os.Getenv("TEST_IMAGE_NAME")
if imageName == "" {
imageName = "vmware/base-fluentd-operator"
}
imageTag := os.Getenv("TEST_IMAGE_TAG")
if imageTag != "" {
imageName = fmt.Sprintf("%s:%s", imageName, imageTag)
}

req := testcontainers.ContainerRequest{
Image: "vmware/base-fluentd-operator:latest",
Image: imageName,
Env: map[string]string{
"FLUENTD_OPT": "--no-supervisor",
},
Expand Down Expand Up @@ -64,6 +75,7 @@ func TestFluentd(t *testing.T) {
NetworkMode: "host",
WaitingFor: wait.ForLog("Found configuration file: /fluentd/etc/fluent.conf"),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
Expand All @@ -77,36 +89,57 @@ func TestFluentd(t *testing.T) {
}
}()
startReceiverServer()
time.Sleep(15 * time.Second)
time.Sleep(30 * time.Second)
mu.Lock()
assert.Equal(counterTotal, counterOutput)
mu.Unlock()
}

func startReceiverServer() {
server := &http.Server{
Addr: ":9090",
Addr: "0.0.0.0:9090",
}
http.HandleFunc("/", printLogs)
go server.ListenAndServe()
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not start server: %s", err.Error())
}
}()

}

func printLogs(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := io.ReadAll(r.Body)
tagName := r.Header["Tag"][0]
if err != nil {
log.Printf("Error reading request body: %v", err)
return
}

tags, ok := r.Header["Tag"]
if !ok || len(tags) == 0 {
http.Error(w, "Tag header not found", http.StatusBadRequest)
}
tagName := tags[0]

bodyString := string(bodyBytes)
log.Println("Received data for tag: " + tagName)
b, err := os.ReadFile("test/results/" + tagName + ".out") // just pass the file name
log.Printf("Received data for tag: %s", tagName)

b, err := os.ReadFile(fmt.Sprintf("test/results/%s.out", tagName))
if err != nil {
log.Fatal(err)
log.Printf("Error reading result file for tag %s: %v", tagName, err)
}
str := string(b) // convert content to a 'string'

str := string(b)
log.Printf("Result from file: %s", str)

if str != bodyString {
log.Fatal("Unmatch for tag " + tagName)
} else {
mu.Lock()
counterOutput++
mu.Unlock()
log.Println("Matching results for tag " + tagName + " and counter value: " + fmt.Sprint(counterOutput))
log.Printf("Unmatch for tag %s", tagName)
http.Error(w, "Mismatched data", http.StatusBadRequest)
return
}

mu.Lock()
defer mu.Unlock() // Always release the lock
counterOutput++
log.Printf("Matching results for tag %s and counter value: %d", tagName, counterOutput)
}
2 changes: 1 addition & 1 deletion base-image/test/ci.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<match **>
@type http
endpoint http://localhost:9090/
endpoint http://host.docker.internal:9090/
json_array true
content_type json
headers_from_placeholders {"tag":"${tag}"}
Expand Down

0 comments on commit 905caf1

Please sign in to comment.