mirror of
https://github.com/argoproj/argo-cd.git
synced 2026-02-20 01:28:45 +01:00
chore: enable noctx linter (#24765)
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
@@ -101,7 +101,7 @@ func testProject() *v1alpha1.AppProject {
|
||||
func TestHandlerFeatureIsEnabled(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp()), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -204,7 +204,7 @@ func TestHandlerFeatureProjectIsEnabled(t *testing.T) {
|
||||
}
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(objects...), settingsMgr, tt.namespace, []string{})
|
||||
rr := httptest.NewRecorder()
|
||||
req, err := http.NewRequest(http.MethodGet, tt.apiEndPoint, http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, tt.apiEndPoint, http.NoBody)
|
||||
require.NoError(t, err)
|
||||
handler.ServeHTTP(rr, req)
|
||||
require.Equal(t, tt.response, rr.Result().StatusCode)
|
||||
@@ -226,7 +226,7 @@ func TestHandlerNamespacesIsEnabled(t *testing.T) {
|
||||
t.Run("Application in allowed namespace", func(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp2()), settingsMgr, "default", []string{"argocd-test"})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&namespace=argocd-test", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&namespace=argocd-test", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -247,7 +247,7 @@ func TestHandlerNamespacesIsEnabled(t *testing.T) {
|
||||
t.Run("Application in disallowed namespace", func(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp2()), settingsMgr, "default", []string{"argocd-test"})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&namespace=kube-system", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&namespace=kube-system", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -264,7 +264,7 @@ func TestHandlerNamespacesIsEnabled(t *testing.T) {
|
||||
t.Run("Request with illegal namespace", func(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp2()), settingsMgr, "default", []string{"argocd-test"})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&namespace=kube()system", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&namespace=kube()system", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -277,7 +277,7 @@ func TestHandlerNamespacesIsEnabled(t *testing.T) {
|
||||
func TestHandlerFeatureIsEnabledKeepFullRevisionIsEnabled(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp3()), settingsMgr, "argocd-test", []string{""})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&revision=true&keepFullRevision=true", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&revision=true&keepFullRevision=true", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -298,7 +298,7 @@ func TestHandlerFeatureIsEnabledKeepFullRevisionIsEnabled(t *testing.T) {
|
||||
func TestHandlerFeatureIsEnabledKeepFullRevisionIsDisabled(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp3()), settingsMgr, "argocd-test", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&revision=true&keepFullRevision=false", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&revision=true&keepFullRevision=false", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -319,7 +319,7 @@ func TestHandlerFeatureIsEnabledKeepFullRevisionIsDisabled(t *testing.T) {
|
||||
func TestHandlerFeatureIsEnabledKeepFullRevisionAndWidthIsEnabled(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp3()), settingsMgr, "argocd-test", []string{""})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&revision=true&keepFullRevision=true&width=500", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&revision=true&keepFullRevision=true&width=500", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -393,7 +393,7 @@ func createApplicationsWithName(appCombo, projectName []string, namespace string
|
||||
func TestHandlerFeatureIsEnabledRevisionIsEnabled(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp()), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -417,7 +417,7 @@ func TestHandlerRevisionIsEnabledNoOperationState(t *testing.T) {
|
||||
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(app), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -441,7 +441,7 @@ func TestHandlerRevisionIsEnabledShortCommitSHA(t *testing.T) {
|
||||
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(app), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -458,7 +458,7 @@ func TestHandlerRevisionIsEnabledMultipleSources(t *testing.T) {
|
||||
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(app), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&revision=true", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -475,7 +475,7 @@ func TestHandlerFeatureIsDisabled(t *testing.T) {
|
||||
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewSimpleClientset(argoCDCmDisabled, argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp()), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -496,7 +496,7 @@ func TestHandlerFeatureIsDisabled(t *testing.T) {
|
||||
func TestHandlerApplicationNameInBadgeIsEnabled(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewSimpleClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp()), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app&showAppName=true", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app&showAppName=true", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -523,7 +523,7 @@ func TestHandlerApplicationNameInBadgeIsEnabled(t *testing.T) {
|
||||
func TestHandlerApplicationNameInBadgeIsDisabled(t *testing.T) {
|
||||
settingsMgr := settings.NewSettingsManager(t.Context(), fake.NewSimpleClientset(argoCDCm(), argoCDSecret()), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(testApp()), settingsMgr, "default", []string{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/badge?name=test-app", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/api/badge?name=test-app", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
func TestValidateHeaders(t *testing.T) {
|
||||
t.Run("will build RequestResources successfully", func(t *testing.T) {
|
||||
// given
|
||||
r, err := http.NewRequest(http.MethodGet, "http://null", http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://null", http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDApplicationName, "namespace:app-name")
|
||||
r.Header.Add(extension.HeaderArgoCDProjectName, "project-name")
|
||||
@@ -45,7 +45,7 @@ func TestValidateHeaders(t *testing.T) {
|
||||
})
|
||||
t.Run("will return error if application is malformatted", func(t *testing.T) {
|
||||
// given
|
||||
r, err := http.NewRequest(http.MethodGet, "http://null", http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://null", http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDApplicationName, "no-namespace")
|
||||
|
||||
@@ -58,7 +58,7 @@ func TestValidateHeaders(t *testing.T) {
|
||||
})
|
||||
t.Run("will return error if application header is missing", func(t *testing.T) {
|
||||
// given
|
||||
r, err := http.NewRequest(http.MethodGet, "http://null", http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://null", http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDProjectName, "project-name")
|
||||
|
||||
@@ -71,7 +71,7 @@ func TestValidateHeaders(t *testing.T) {
|
||||
})
|
||||
t.Run("will return error if project header is missing", func(t *testing.T) {
|
||||
// given
|
||||
r, err := http.NewRequest(http.MethodGet, "http://null", http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://null", http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDApplicationName, "namespace:app-name")
|
||||
|
||||
@@ -84,7 +84,7 @@ func TestValidateHeaders(t *testing.T) {
|
||||
})
|
||||
t.Run("will return error if invalid namespace", func(t *testing.T) {
|
||||
// given
|
||||
r, err := http.NewRequest(http.MethodGet, "http://null", http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://null", http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDApplicationName, "bad%namespace:app-name")
|
||||
r.Header.Add(extension.HeaderArgoCDProjectName, "project-name")
|
||||
@@ -98,7 +98,7 @@ func TestValidateHeaders(t *testing.T) {
|
||||
})
|
||||
t.Run("will return error if invalid app name", func(t *testing.T) {
|
||||
// given
|
||||
r, err := http.NewRequest(http.MethodGet, "http://null", http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://null", http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDApplicationName, "namespace:bad@app")
|
||||
r.Header.Add(extension.HeaderArgoCDProjectName, "project-name")
|
||||
@@ -112,7 +112,7 @@ func TestValidateHeaders(t *testing.T) {
|
||||
})
|
||||
t.Run("will return error if invalid project name", func(t *testing.T) {
|
||||
// given
|
||||
r, err := http.NewRequest(http.MethodGet, "http://null", http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://null", http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDApplicationName, "namespace:app")
|
||||
r.Header.Add(extension.HeaderArgoCDProjectName, "bad^project")
|
||||
@@ -390,7 +390,7 @@ func TestCallExtension(t *testing.T) {
|
||||
}
|
||||
newExtensionRequest := func(t *testing.T, method, url string) *http.Request {
|
||||
t.Helper()
|
||||
r, err := http.NewRequest(method, url, http.NoBody)
|
||||
r, err := http.NewRequestWithContext(t.Context(), method, url, http.NoBody)
|
||||
require.NoError(t, err, "error initializing request")
|
||||
r.Header.Add(extension.HeaderArgoCDApplicationName, "namespace:app-name")
|
||||
r.Header.Add(extension.HeaderArgoCDProjectName, defaultProjectName)
|
||||
|
||||
@@ -286,21 +286,22 @@ func TestHandlerConstructLogoutURL(t *testing.T) {
|
||||
nonOidcTokenHeader["Cookie"] = []string{"argocd.token=" + nonOidcToken}
|
||||
invalidHeader := make(map[string][]string)
|
||||
invalidHeader["Cookie"] = []string{"argocd.token=" + invalidToken}
|
||||
ctx := t.Context()
|
||||
|
||||
oidcRequest, err := http.NewRequest(http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
oidcRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
oidcRequest.Header = oidcTokenHeader
|
||||
nonoidcRequest, err := http.NewRequest(http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
nonoidcRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
nonoidcRequest.Header = nonOidcTokenHeader
|
||||
nonoidcRequestOnSecondHost, err := http.NewRequest(http.MethodGet, "http://argocd.my-corp.tld/api/logout", http.NoBody)
|
||||
nonoidcRequestOnSecondHost, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://argocd.my-corp.tld/api/logout", http.NoBody)
|
||||
assert.NoError(t, err)
|
||||
nonoidcRequestOnSecondHost.Header = nonOidcTokenHeader
|
||||
assert.NoError(t, err)
|
||||
requestWithInvalidToken, err := http.NewRequest(http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
requestWithInvalidToken, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
requestWithInvalidToken.Header = invalidHeader
|
||||
invalidRequest, err := http.NewRequest(http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
invalidRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:4000/api/logout", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
|
||||
@@ -495,8 +495,9 @@ func (server *ArgoCDServer) logInClusterWarnings() error {
|
||||
func startListener(host string, port int) (net.Listener, error) {
|
||||
var conn net.Listener
|
||||
var realErr error
|
||||
lc := net.ListenConfig{}
|
||||
_ = wait.ExponentialBackoff(backoff, func() (bool, error) {
|
||||
conn, realErr = net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
||||
conn, realErr = lc.Listen(context.Background(), "tcp", fmt.Sprintf("%s:%d", host, port))
|
||||
if realErr != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -108,14 +108,14 @@ func Test_StaticHeaders(t *testing.T) {
|
||||
// Allow server startup
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
client := http.Client{}
|
||||
url := fmt.Sprintf("http://127.0.0.1:%d/test.html", s.ListenPort)
|
||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, url, http.NoBody)
|
||||
require.NoError(t, err)
|
||||
resp, err := client.Do(req)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "sameorigin", resp.Header.Get("X-Frame-Options"))
|
||||
assert.Equal(t, "frame-ancestors 'self';", resp.Header.Get("Content-Security-Policy"))
|
||||
require.NoError(t, resp.Body.Close())
|
||||
}
|
||||
|
||||
// Test custom policy for X-Frame-Options and Content-Security-Policy
|
||||
@@ -137,14 +137,14 @@ func Test_StaticHeaders(t *testing.T) {
|
||||
// Allow server startup
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
client := http.Client{}
|
||||
url := fmt.Sprintf("http://127.0.0.1:%d/test.html", s.ListenPort)
|
||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, url, http.NoBody)
|
||||
require.NoError(t, err)
|
||||
resp, err := client.Do(req)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "deny", resp.Header.Get("X-Frame-Options"))
|
||||
assert.Equal(t, "frame-ancestors 'none';", resp.Header.Get("Content-Security-Policy"))
|
||||
require.NoError(t, resp.Body.Close())
|
||||
}
|
||||
|
||||
// Test disabled X-Frame-Options and Content-Security-Policy
|
||||
@@ -169,13 +169,13 @@ func Test_StaticHeaders(t *testing.T) {
|
||||
// Allow server startup
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
client := http.Client{}
|
||||
url := fmt.Sprintf("http://127.0.0.1:%d/test.html", s.ListenPort)
|
||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, url, http.NoBody)
|
||||
require.NoError(t, err)
|
||||
resp, err := client.Do(req)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, resp.Header.Get("X-Frame-Options"))
|
||||
assert.Empty(t, resp.Header.Get("Content-Security-Policy"))
|
||||
require.NoError(t, resp.Body.Close())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user