Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: image tests #418

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading