From 4d582dd9d9a63b2182231b8a6f12d290a2321a89 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Tue, 17 Sep 2024 10:28:55 +0100 Subject: [PATCH 1/8] Added WithMacAPIVersion option to NewNginxClient --- client/nginx.go | 11 +++++++++ client/nginx_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/client/nginx.go b/client/nginx.go index 7a76d41c..bc5ae445 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -597,6 +597,17 @@ func WithTimeout(duration time.Duration) Option { } } +// WithMaxAPIVersion sets the flag to use the max API version. +func WithMaxAPIVersion() Option { + return func(o *NginxClient) { + version, err := o.GetMaxAPIVersion() + if err != nil { + return + } + o.apiVersion = version + } +} + // NewNginxClient creates a new NginxClient. func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) { c := &NginxClient{ diff --git a/client/nginx_test.go b/client/nginx_test.go index 0ad650c9..70e36183 100644 --- a/client/nginx_test.go +++ b/client/nginx_test.go @@ -623,6 +623,63 @@ func TestClientWithHTTPClient(t *testing.T) { } } +func TestClientWithMaxAPI(t *testing.T) { + t.Parallel() + + tests := []struct { + apiVersions string + expected int + }{ + { + apiVersions: `[4, 5, 6, 7, 8, 9, 25]`, + expected: APIVersion, + }, + { + apiVersions: ``, + expected: APIVersion, + }, + { + apiVersions: `[4, 5, 6, 7]`, + expected: 7, + }, + { + apiVersions: `[""]`, + expected: APIVersion, + }, + } + + for _, test := range tests { + // Test creating a new client with max API version + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.RequestURI == "/": + _, err := w.Write([]byte(test.apiVersions)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + default: + _, err := w.Write([]byte(`{}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + } + })) + defer ts.Close() + + client, err := NewNginxClient(ts.URL, WithMaxAPIVersion()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if client == nil { + t.Fatalf("client is nil") + } + if client.apiVersion != test.expected { + t.Fatalf("expected client.apiVersion to be %v, but got %v", test.expected, client.apiVersion) + } + } + +} + func TestGetStats_NoStreamEndpoint(t *testing.T) { tests := []struct { ctx context.Context From 03d0a4df56d040359caa528ebfd0bf8e31c52ae3 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Tue, 17 Sep 2024 10:49:28 +0100 Subject: [PATCH 2/8] Change description in comment --- .idea/.gitignore | 8 ++++++++ .idea/vcs.xml | 6 ++++++ client/nginx.go | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/client/nginx.go b/client/nginx.go index bc5ae445..181bd4ec 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -597,7 +597,7 @@ func WithTimeout(duration time.Duration) Option { } } -// WithMaxAPIVersion sets the flag to use the max API version. +// WithMaxAPIVersion sets the API version to the max API version. func WithMaxAPIVersion() Option { return func(o *NginxClient) { version, err := o.GetMaxAPIVersion() From 860013f4d750b8faf90af387583b5c2267bf3ec9 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Tue, 17 Sep 2024 10:53:55 +0100 Subject: [PATCH 3/8] Remove ide files --- .idea/.gitignore | 8 -------- .idea/vcs.xml | 6 ------ 2 files changed, 14 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddf..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From b8646d87a1d94535dccc89a2ed9ad1e2b557f263 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Tue, 17 Sep 2024 10:56:07 +0100 Subject: [PATCH 4/8] Add GoLand settings to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 7b57f9e7..0e534a13 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ # Visual Studio Code settings .vscode +# Goland settings +.idea/ + dist From d481ca7ae35f83d666383c99e04e32e674f869fc Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Tue, 17 Sep 2024 11:16:03 +0100 Subject: [PATCH 5/8] Run linter --- client/nginx_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client/nginx_test.go b/client/nginx_test.go index 70e36183..d25de14a 100644 --- a/client/nginx_test.go +++ b/client/nginx_test.go @@ -677,7 +677,6 @@ func TestClientWithMaxAPI(t *testing.T) { t.Fatalf("expected client.apiVersion to be %v, but got %v", test.expected, client.apiVersion) } } - } func TestGetStats_NoStreamEndpoint(t *testing.T) { From 516def81334b79951ca2860affbccfc3563bdc16 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Wed, 18 Sep 2024 09:33:24 +0100 Subject: [PATCH 6/8] Use t.Run in test --- client/nginx_test.go | 59 +++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/client/nginx_test.go b/client/nginx_test.go index d25de14a..b0815a9a 100644 --- a/client/nginx_test.go +++ b/client/nginx_test.go @@ -627,55 +627,62 @@ func TestClientWithMaxAPI(t *testing.T) { t.Parallel() tests := []struct { + name string apiVersions string expected int }{ { + name: "Test 1: API versions contains invalid version", apiVersions: `[4, 5, 6, 7, 8, 9, 25]`, expected: APIVersion, }, { + name: "Test 2: No API versions, default API Version is used", apiVersions: ``, expected: APIVersion, }, { + name: "Test 3: API version lower than default", apiVersions: `[4, 5, 6, 7]`, expected: 7, }, { + name: "Test 4: No API versions, default API version is used", apiVersions: `[""]`, expected: APIVersion, }, } - for _, test := range tests { - // Test creating a new client with max API version - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.RequestURI == "/": - _, err := w.Write([]byte(test.apiVersions)) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - default: - _, err := w.Write([]byte(`{}`)) - if err != nil { - t.Fatalf("unexpected error: %v", err) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Test creating a new client with max API version + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.RequestURI == "/": + _, err := w.Write([]byte(tt.apiVersions)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + default: + _, err := w.Write([]byte(`{}`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } } - } - })) - defer ts.Close() + })) + defer ts.Close() - client, err := NewNginxClient(ts.URL, WithMaxAPIVersion()) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if client == nil { - t.Fatalf("client is nil") - } - if client.apiVersion != test.expected { - t.Fatalf("expected client.apiVersion to be %v, but got %v", test.expected, client.apiVersion) - } + client, err := NewNginxClient(ts.URL, WithMaxAPIVersion()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if client == nil { + t.Fatalf("client is nil") + } + if client.apiVersion != tt.expected { + t.Fatalf("expected client.apiVersion to be %v, but got %v", tt.expected, client.apiVersion) + } + }) } } From 160185de2a50de067128aa3fad4bc5fa5a1d44e3 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Wed, 18 Sep 2024 10:05:46 +0100 Subject: [PATCH 7/8] Linting fix --- client/nginx_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/nginx_test.go b/client/nginx_test.go index b0815a9a..80fc50c3 100644 --- a/client/nginx_test.go +++ b/client/nginx_test.go @@ -625,7 +625,6 @@ func TestClientWithHTTPClient(t *testing.T) { func TestClientWithMaxAPI(t *testing.T) { t.Parallel() - tests := []struct { name string apiVersions string @@ -655,6 +654,7 @@ func TestClientWithMaxAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + t.Parallel() // Test creating a new client with max API version ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { From 07c846c879d08e0ef839da4935bdc044cef22bd6 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Tue, 24 Sep 2024 13:27:42 +0100 Subject: [PATCH 8/8] resolve conflicts --- client/nginx.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/nginx.go b/client/nginx.go index d7b2826b..7d16420a 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -591,7 +591,9 @@ func WithCheckAPI() Option { // WithMaxAPIVersion sets the API version to the max API version. func WithMaxAPIVersion() Option { return func(o *NginxClient) { - version, err := o.GetMaxAPIVersion() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + version, err := o.GetMaxAPIVersion(ctx) if err != nil { return }