Skip to content

Commit

Permalink
Extend test to verify custom kubeconfigPath is used
Browse files Browse the repository at this point in the history
  • Loading branch information
avorima committed Jan 12, 2024
1 parent f26bb8e commit bd93050
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions cmd/kubeadm/app/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ limitations under the License.
package cmd

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -54,12 +57,12 @@ kind: ClusterConfiguration
controlPlaneEndpoint: "3.4.5.6"
`, kubeadmapiv1.SchemeGroupVersion.String(), expectedCRISocket)

const testKubeconfigData = `---
const testKubeconfigDataFormat = `---
apiVersion: v1
clusters:
- name: foo-cluster
cluster:
server: https://localhost:8080
server: %s
contexts:
- name: foo-context
context:
Expand Down Expand Up @@ -365,8 +368,15 @@ func expectedInitIgnorePreflightErrors(expectedItems ...string) func(t *testing.
}

func TestInitDataClientWithNonDefaultKubeconfig(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}))
defer ts.Close()

kubeconfigPath := filepath.Join(t.TempDir(), "custom.conf")
if err := os.WriteFile(kubeconfigPath, []byte(testKubeconfigData), 0o600); err != nil {
if err := os.WriteFile(kubeconfigPath, []byte(fmt.Sprintf(testKubeconfigDataFormat, ts.URL)), 0o600); err != nil {
t.Fatalf("os.WriteFile returned unexpected error: %v", err)
}

Expand All @@ -381,8 +391,13 @@ func TestInitDataClientWithNonDefaultKubeconfig(t *testing.T) {
t.Fatalf("newInitData returned unexpected error: %v", err)
}

// This only works if the client that is constructed from the kubeconfig is not used.
if _, err := data.Client(); err != nil {
client, err := data.Client()
if err != nil {
t.Fatalf("data.Client returned unexpected error: %v", err)
}

result := client.Discovery().RESTClient().Verb("HEAD").Do(context.Background())
if err := result.Error(); err != nil {
t.Fatalf("REST client request returned unexpected error: %v", err)
}
}

0 comments on commit bd93050

Please sign in to comment.