From 76e56cf5ab291106e8a26879a00926baac4ca8fa Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Sat, 23 Dec 2023 15:30:16 -0600 Subject: [PATCH 1/8] Update configuration_test.go --- configuration_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/configuration_test.go b/configuration_test.go index f4b8beb..c4e1441 100644 --- a/configuration_test.go +++ b/configuration_test.go @@ -114,6 +114,100 @@ func TestClient_GetConfiguration(t *testing.T) { }) } } +func TestClient_GetConfigurationWithLabel(t *testing.T) { + tests := []struct { + name string + label string + application string + profiles []string + checker func(*testing.T, *http.Request) + response *http.Response + expected cloudconfigclient.Source + err error + }{ + { + name: "Get Config", + label: "master" + application: "appName", + profiles: []string{"profile"}, + checker: func(t *testing.T, request *http.Request) { + require.Equal(t, "http://localhost:8888/appName/profile", request.URL.String()) + }, + response: NewMockHttpResponse(http.StatusOK, configurationSource), + expected: cloudconfigclient.Source{ + Name: "testConfig", + Profiles: []string{"profile"}, + PropertySources: []cloudconfigclient.PropertySource{{Name: "test", Source: map[string]interface{}{"field1": "value1", "field2": float64(1)}}}, + }, + }, + { + name: "Multiple Profiles", + label: "master" + application: "appName", + profiles: []string{"profile1", "profile2", "profile3"}, + checker: func(t *testing.T, request *http.Request) { + require.Equal(t, "http://localhost:8888/appName/profile1,profile2,profile3", request.URL.String()) + }, + response: NewMockHttpResponse(http.StatusOK, configurationSource), + expected: cloudconfigclient.Source{ + Name: "testConfig", + Profiles: []string{"profile"}, + PropertySources: []cloudconfigclient.PropertySource{{Name: "test", Source: map[string]interface{}{"field1": "value1", "field2": float64(1)}}}, + }, + }, + { + name: "Not Found", + label: "master" + application: "appName", + profiles: []string{"profile"}, + response: NewMockHttpResponse(http.StatusNotFound, ""), + err: errors.New("failed to find configuration for application appName with profiles [profile]"), + }, + { + name: "Server Error", + label: "master" + application: "appName", + profiles: []string{"profile"}, + response: NewMockHttpResponse(http.StatusInternalServerError, ""), + err: errors.New("server responded with status code '500' and body ''"), + }, + { + name: "No Response Body", + label: "master" + application: "appName", + profiles: []string{"profile"}, + response: NewMockHttpResponse(http.StatusOK, ""), + err: errors.New("failed to decode response from url: EOF"), + }, + { + name: "HTTP Error", + label: "master" + application: "appName", + profiles: []string{"profile"}, + err: errors.New("failed to retrieve from http://localhost:8888/appName/profile: Get \"http://localhost:8888/appName/profile\": http: RoundTripper implementation (cloudconfigclient_test.RoundTripFunc) returned a nil *Response with a nil error"), + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + httpClient := NewMockHttpClient(func(req *http.Request) *http.Response { + if test.checker != nil { + test.checker(t, req) + } + return test.response + }) + client, err := cloudconfigclient.New(cloudconfigclient.Local(httpClient, "http://localhost:8888")) + require.NoError(t, err) + configuration, err := client.GetConfigurationWithLabel(test.label, test.application, test.profiles...) + if err != nil { + require.Error(t, err) + require.Equal(t, test.err.Error(), err.Error()) + } else { + require.NoError(t, err) + require.Equal(t, test.expected, configuration) + } + }) + } +} func TestSource_GetPropertySource(t *testing.T) { source := cloudconfigclient.Source{ From 97a500f47570b291e0780b7d1402177eda3a3f85 Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Sat, 23 Dec 2023 15:31:00 -0600 Subject: [PATCH 2/8] Update configuration.go --- configuration.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/configuration.go b/configuration.go index dd159ab..2de54fd 100644 --- a/configuration.go +++ b/configuration.go @@ -171,6 +171,23 @@ func (c *Client) GetConfiguration(applicationName string, profiles ...string) (S return Source{}, fmt.Errorf("failed to find configuration for application %s with profiles %s", applicationName, profiles) } +// GetConfiguration retrieves the configurations/property sources of an application based on the name of the application +// and the profiles of the application and the label. +func (c *Client) GetConfigurationWithLabel(label string, applicationName string, profiles ...string) (Source, error) { + var source Source + paths := []string{applicationName, joinProfiles(profiles), label} + for _, client := range c.clients { + if err := client.GetResource(paths, nil, &source); err != nil { + if errors.Is(err, ErrResourceNotFound) { + continue + } + return Source{}, err + } + return source, nil + } + return Source{}, fmt.Errorf("failed to find configuration for application %s with profiles %s", applicationName, profiles) +} + func joinProfiles(profiles []string) string { return strings.Join(profiles, ",") } From 6652e0a02088a0d6b88e1686178e1ddf6d2c97bd Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Sat, 23 Dec 2023 15:33:23 -0600 Subject: [PATCH 3/8] Update configuration_test.go --- configuration_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/configuration_test.go b/configuration_test.go index c4e1441..228c89e 100644 --- a/configuration_test.go +++ b/configuration_test.go @@ -131,7 +131,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { application: "appName", profiles: []string{"profile"}, checker: func(t *testing.T, request *http.Request) { - require.Equal(t, "http://localhost:8888/appName/profile", request.URL.String()) + require.Equal(t, "http://localhost:8888/appName/profile/master", request.URL.String()) }, response: NewMockHttpResponse(http.StatusOK, configurationSource), expected: cloudconfigclient.Source{ @@ -146,7 +146,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { application: "appName", profiles: []string{"profile1", "profile2", "profile3"}, checker: func(t *testing.T, request *http.Request) { - require.Equal(t, "http://localhost:8888/appName/profile1,profile2,profile3", request.URL.String()) + require.Equal(t, "http://localhost:8888/appName/profile1,profile2,profile3/master", request.URL.String()) }, response: NewMockHttpResponse(http.StatusOK, configurationSource), expected: cloudconfigclient.Source{ @@ -184,7 +184,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { label: "master" application: "appName", profiles: []string{"profile"}, - err: errors.New("failed to retrieve from http://localhost:8888/appName/profile: Get \"http://localhost:8888/appName/profile\": http: RoundTripper implementation (cloudconfigclient_test.RoundTripFunc) returned a nil *Response with a nil error"), + err: errors.New("failed to retrieve from http://localhost:8888/appName/profile/master: Get \"http://localhost:8888/appName/profile/master\": http: RoundTripper implementation (cloudconfigclient_test.RoundTripFunc) returned a nil *Response with a nil error"), }, } for _, test := range tests { From d9185a6b0f567921face7d13a90cb74cb67f9db7 Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Sat, 23 Dec 2023 15:36:43 -0600 Subject: [PATCH 4/8] Update configuration.go --- configuration.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configuration.go b/configuration.go index 2de54fd..7f1d27c 100644 --- a/configuration.go +++ b/configuration.go @@ -185,7 +185,7 @@ func (c *Client) GetConfigurationWithLabel(label string, applicationName string, } return source, nil } - return Source{}, fmt.Errorf("failed to find configuration for application %s with profiles %s", applicationName, profiles) + return Source{}, fmt.Errorf("failed to find configuration for application %s with profiles %s and label %s", applicationName, profiles, label) } func joinProfiles(profiles []string) string { From 52d58a207d6b9638af55b96e74b251d5259eb772 Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Sat, 23 Dec 2023 18:20:51 -0600 Subject: [PATCH 5/8] Update configuration.go --- configuration.go | 1 + 1 file changed, 1 insertion(+) diff --git a/configuration.go b/configuration.go index 7f1d27c..1818039 100644 --- a/configuration.go +++ b/configuration.go @@ -152,6 +152,7 @@ type Configuration interface { // GetConfiguration retrieves the configurations/property sources of an application based on the name of the application // and the profiles of the application. GetConfiguration(applicationName string, profiles ...string) (Source, error) + GetConfigurationWithLabel(label string, applicationName string, profiles ...string) (Source, error) } // GetConfiguration retrieves the configurations/property sources of an application based on the name of the application From f5c95ef5515f0f1536ca80b9af383d56fe973514 Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Sat, 23 Dec 2023 18:22:16 -0600 Subject: [PATCH 6/8] Update configuration.go --- configuration.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/configuration.go b/configuration.go index 1818039..4e8d191 100644 --- a/configuration.go +++ b/configuration.go @@ -152,6 +152,8 @@ type Configuration interface { // GetConfiguration retrieves the configurations/property sources of an application based on the name of the application // and the profiles of the application. GetConfiguration(applicationName string, profiles ...string) (Source, error) + // GetConfigurationWithLabel retrieves the configurations/property sources of an application based on the name of the application + // and the profiles of the application and the label. GetConfigurationWithLabel(label string, applicationName string, profiles ...string) (Source, error) } @@ -172,7 +174,7 @@ func (c *Client) GetConfiguration(applicationName string, profiles ...string) (S return Source{}, fmt.Errorf("failed to find configuration for application %s with profiles %s", applicationName, profiles) } -// GetConfiguration retrieves the configurations/property sources of an application based on the name of the application +// GetConfigurationWithLabel retrieves the configurations/property sources of an application based on the name of the application // and the profiles of the application and the label. func (c *Client) GetConfigurationWithLabel(label string, applicationName string, profiles ...string) (Source, error) { var source Source From 1a5f0e9151608abc3ba1f2cef9e56b8462a34e0d Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Tue, 2 Jan 2024 17:17:20 -0600 Subject: [PATCH 7/8] Update configuration_test.go --- configuration_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/configuration_test.go b/configuration_test.go index 228c89e..404894d 100644 --- a/configuration_test.go +++ b/configuration_test.go @@ -127,7 +127,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { }{ { name: "Get Config", - label: "master" + label: "master", application: "appName", profiles: []string{"profile"}, checker: func(t *testing.T, request *http.Request) { @@ -142,7 +142,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { }, { name: "Multiple Profiles", - label: "master" + label: "master", application: "appName", profiles: []string{"profile1", "profile2", "profile3"}, checker: func(t *testing.T, request *http.Request) { @@ -157,7 +157,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { }, { name: "Not Found", - label: "master" + label: "master", application: "appName", profiles: []string{"profile"}, response: NewMockHttpResponse(http.StatusNotFound, ""), @@ -165,7 +165,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { }, { name: "Server Error", - label: "master" + label: "master", application: "appName", profiles: []string{"profile"}, response: NewMockHttpResponse(http.StatusInternalServerError, ""), @@ -173,7 +173,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { }, { name: "No Response Body", - label: "master" + label: "master", application: "appName", profiles: []string{"profile"}, response: NewMockHttpResponse(http.StatusOK, ""), @@ -181,7 +181,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { }, { name: "HTTP Error", - label: "master" + label: "master", application: "appName", profiles: []string{"profile"}, err: errors.New("failed to retrieve from http://localhost:8888/appName/profile/master: Get \"http://localhost:8888/appName/profile/master\": http: RoundTripper implementation (cloudconfigclient_test.RoundTripFunc) returned a nil *Response with a nil error"), From f940866da22e67b287b019a53c9c91ee467b633e Mon Sep 17 00:00:00 2001 From: TristanHorlamus <57843741+TristanHorlamus@users.noreply.github.com> Date: Tue, 23 Jan 2024 12:13:50 -0600 Subject: [PATCH 8/8] Update configuration_test.go --- configuration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configuration_test.go b/configuration_test.go index 404894d..1302159 100644 --- a/configuration_test.go +++ b/configuration_test.go @@ -161,7 +161,7 @@ func TestClient_GetConfigurationWithLabel(t *testing.T) { application: "appName", profiles: []string{"profile"}, response: NewMockHttpResponse(http.StatusNotFound, ""), - err: errors.New("failed to find configuration for application appName with profiles [profile]"), + err: errors.New("failed to find configuration for application appName with profiles [profile] and label master"), }, { name: "Server Error",