Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sjberman committed Dec 20, 2024
1 parent b83eabf commit 09b399d
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions internal/mode/static/nginx/config/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config_test

import (
"fmt"
"sort"
"testing"

Expand Down Expand Up @@ -144,15 +143,14 @@ func TestGenerate(t *testing.T) {

files := generator.Generate(conf)

g.Expect(files).To(HaveLen(17))
g.Expect(files).To(HaveLen(16))
arrange := func(i, j int) bool {
return files[i].Path < files[j].Path
}
sort.Slice(files, arrange)

/*
Order of files:
/etc/nginx/conf.d/config-version.conf
/etc/nginx/conf.d/http.conf
/etc/nginx/conf.d/matches.json
/etc/nginx/includes/http_snippet1.conf
Expand All @@ -172,13 +170,8 @@ func TestGenerate(t *testing.T) {
*/

g.Expect(files[0].Type).To(Equal(file.TypeRegular))
g.Expect(files[0].Path).To(Equal("/etc/nginx/conf.d/config-version.conf"))
configVersion := string(files[0].Content)
g.Expect(configVersion).To(ContainSubstring(fmt.Sprintf("return 200 %d", conf.Version)))

g.Expect(files[1].Type).To(Equal(file.TypeRegular))
g.Expect(files[1].Path).To(Equal("/etc/nginx/conf.d/http.conf"))
httpCfg := string(files[1].Content) // converting to string so that on failure gomega prints strings not byte arrays
g.Expect(files[0].Path).To(Equal("/etc/nginx/conf.d/http.conf"))
httpCfg := string(files[0].Content) // converting to string so that on failure gomega prints strings not byte arrays
// Note: this only verifies that Generate() returns a byte array with upstream, server, and split_client blocks.
// It does not test the correctness of those blocks. That functionality is covered by other tests in this package.
g.Expect(httpCfg).To(ContainSubstring("listen 80"))
Expand All @@ -195,66 +188,66 @@ func TestGenerate(t *testing.T) {
g.Expect(httpCfg).To(ContainSubstring("include /etc/nginx/includes/http_snippet1.conf;"))
g.Expect(httpCfg).To(ContainSubstring("include /etc/nginx/includes/http_snippet2.conf;"))

g.Expect(files[2].Path).To(Equal("/etc/nginx/conf.d/matches.json"))
g.Expect(files[1].Path).To(Equal("/etc/nginx/conf.d/matches.json"))

g.Expect(files[2].Type).To(Equal(file.TypeRegular))
g.Expect(files[1].Type).To(Equal(file.TypeRegular))
expString := "{}"
g.Expect(string(files[2].Content)).To(Equal(expString))
g.Expect(string(files[1].Content)).To(Equal(expString))

// snippet include files
// content is not checked in this test.
g.Expect(files[3].Path).To(Equal("/etc/nginx/includes/http_snippet1.conf"))
g.Expect(files[4].Path).To(Equal("/etc/nginx/includes/http_snippet2.conf"))
g.Expect(files[5].Path).To(Equal("/etc/nginx/includes/main_snippet1.conf"))
g.Expect(files[6].Path).To(Equal("/etc/nginx/includes/main_snippet2.conf"))
g.Expect(files[2].Path).To(Equal("/etc/nginx/includes/http_snippet1.conf"))
g.Expect(files[3].Path).To(Equal("/etc/nginx/includes/http_snippet2.conf"))
g.Expect(files[4].Path).To(Equal("/etc/nginx/includes/main_snippet1.conf"))
g.Expect(files[5].Path).To(Equal("/etc/nginx/includes/main_snippet2.conf"))

g.Expect(files[7].Path).To(Equal("/etc/nginx/main-includes/deployment_ctx.json"))
deploymentCtx := string(files[7].Content)
g.Expect(files[6].Path).To(Equal("/etc/nginx/main-includes/deployment_ctx.json"))
deploymentCtx := string(files[6].Content)
g.Expect(deploymentCtx).To(ContainSubstring("\"integration\":\"ngf\""))
g.Expect(deploymentCtx).To(ContainSubstring("\"cluster_id\":\"test-uid\""))
g.Expect(deploymentCtx).To(ContainSubstring("\"installation_id\":\"test-uid-replicaSet\""))
g.Expect(deploymentCtx).To(ContainSubstring("\"cluster_node_count\":1"))

g.Expect(files[8].Path).To(Equal("/etc/nginx/main-includes/main.conf"))
mainConfStr := string(files[8].Content)
g.Expect(files[7].Path).To(Equal("/etc/nginx/main-includes/main.conf"))
mainConfStr := string(files[7].Content)
g.Expect(mainConfStr).To(ContainSubstring("load_module modules/ngx_otel_module.so;"))
g.Expect(mainConfStr).To(ContainSubstring("include /etc/nginx/includes/main_snippet1.conf;"))
g.Expect(mainConfStr).To(ContainSubstring("include /etc/nginx/includes/main_snippet2.conf;"))

g.Expect(files[9].Path).To(Equal("/etc/nginx/main-includes/mgmt.conf"))
mgmtConf := string(files[9].Content)
g.Expect(files[8].Path).To(Equal("/etc/nginx/main-includes/mgmt.conf"))
mgmtConf := string(files[8].Content)
g.Expect(mgmtConf).To(ContainSubstring("usage_report endpoint=test-endpoint"))
g.Expect(mgmtConf).To(ContainSubstring("license_token /etc/nginx/secrets/license.jwt"))
g.Expect(mgmtConf).To(ContainSubstring("deployment_context /etc/nginx/main-includes/deployment_ctx.json"))
g.Expect(mgmtConf).To(ContainSubstring("ssl_trusted_certificate /etc/nginx/secrets/mgmt-ca.crt"))
g.Expect(mgmtConf).To(ContainSubstring("ssl_certificate /etc/nginx/secrets/mgmt-tls.crt"))
g.Expect(mgmtConf).To(ContainSubstring("ssl_certificate_key /etc/nginx/secrets/mgmt-tls.key"))

g.Expect(files[10].Path).To(Equal("/etc/nginx/secrets/license.jwt"))
g.Expect(string(files[10].Content)).To(Equal("license"))
g.Expect(files[9].Path).To(Equal("/etc/nginx/secrets/license.jwt"))
g.Expect(string(files[9].Content)).To(Equal("license"))

g.Expect(files[11].Path).To(Equal("/etc/nginx/secrets/mgmt-ca.crt"))
g.Expect(string(files[11].Content)).To(Equal("ca"))
g.Expect(files[10].Path).To(Equal("/etc/nginx/secrets/mgmt-ca.crt"))
g.Expect(string(files[10].Content)).To(Equal("ca"))

g.Expect(files[12].Path).To(Equal("/etc/nginx/secrets/mgmt-tls.crt"))
g.Expect(string(files[12].Content)).To(Equal("cert"))
g.Expect(files[11].Path).To(Equal("/etc/nginx/secrets/mgmt-tls.crt"))
g.Expect(string(files[11].Content)).To(Equal("cert"))

g.Expect(files[13].Path).To(Equal("/etc/nginx/secrets/mgmt-tls.key"))
g.Expect(string(files[13].Content)).To(Equal("key"))
g.Expect(files[12].Path).To(Equal("/etc/nginx/secrets/mgmt-tls.key"))
g.Expect(string(files[12].Content)).To(Equal("key"))

g.Expect(files[14].Path).To(Equal("/etc/nginx/secrets/test-certbundle.crt"))
certBundle := string(files[14].Content)
g.Expect(files[13].Path).To(Equal("/etc/nginx/secrets/test-certbundle.crt"))
certBundle := string(files[13].Content)
g.Expect(certBundle).To(Equal("test-cert"))

g.Expect(files[15]).To(Equal(file.File{
g.Expect(files[14]).To(Equal(file.File{
Type: file.TypeSecret,
Path: "/etc/nginx/secrets/test-keypair.pem",
Content: []byte("test-cert\ntest-key"),
}))

g.Expect(files[16].Path).To(Equal("/etc/nginx/stream-conf.d/stream.conf"))
g.Expect(files[16].Type).To(Equal(file.TypeRegular))
streamCfg := string(files[16].Content)
g.Expect(files[15].Path).To(Equal("/etc/nginx/stream-conf.d/stream.conf"))
g.Expect(files[15].Type).To(Equal(file.TypeRegular))
streamCfg := string(files[15].Content)
g.Expect(streamCfg).To(ContainSubstring("listen unix:/var/run/nginx/app.example.com-443.sock"))
g.Expect(streamCfg).To(ContainSubstring("listen 443"))
g.Expect(streamCfg).To(ContainSubstring("app.example.com unix:/var/run/nginx/app.example.com-443.sock"))
Expand Down

0 comments on commit 09b399d

Please sign in to comment.