Skip to content

Commit

Permalink
add integration test for '--binary-mirror' flag
Browse files Browse the repository at this point in the history
  • Loading branch information
presztak committed Jan 12, 2022
1 parent 4a2c97f commit 20d3c76
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/integration/aaa_download_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ import (
"bytes"
"context"
"crypto/md5"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -247,3 +251,69 @@ func TestDownloadOnlyKic(t *testing.T) {
t.Errorf("failed to verify checksum. checksum of %q does not match remote checksum (%q != %q)", tarball, string(remoteChecksum), string(checksum[:]))
}
}

// createSha256File is a helper function which creates sha256 checksum file from given file
func createSha256File(filePath string) error {
dat, _ := os.ReadFile(filePath)
sum := sha256.Sum256(dat)

f, err := os.Create(filePath + ".sha256")
if err != nil {
return err
}
defer f.Close()

_, err = f.WriteString(fmt.Sprintf("%x", sum[:]))
if err != nil {
return err
}
return nil
}

// TestBinaryMirror tests functionality of --binary-mirror flag
func TestBinaryMirror(t *testing.T) {
profile := UniqueProfileName("binary-mirror")
ctx, cancel := context.WithTimeout(context.Background(), Minutes(10))
defer Cleanup(t, profile, cancel)

tmpDir, err := ioutil.TempDir("", "kb_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

// Start test server which will serve binary files
ts := httptest.NewServer(
http.FileServer(http.Dir(tmpDir)),
)
defer ts.Close()

binaryName := "kubectl"
if runtime.GOOS == "windows" {
binaryName = "kubectl.exe"
}
binaryPath, err := download.Binary(binaryName, constants.DefaultKubernetesVersion, runtime.GOOS, runtime.GOARCH, "")
if err != nil {
t.Errorf("Failed to download binary: %+v", err)
}

newBinaryDir := filepath.Join(tmpDir, constants.DefaultKubernetesVersion, "bin", runtime.GOOS, runtime.GOARCH)
if err := os.MkdirAll(newBinaryDir, os.ModePerm); err != nil {
t.Errorf("Failed to create %s directories", newBinaryDir)
}

newBinaryPath := filepath.Join(newBinaryDir, binaryName)
if err := os.Rename(binaryPath, newBinaryPath); err != nil {
t.Errorf("Failed to move binary file: %+v", err)
}
if err := createSha256File(newBinaryPath); err != nil {
t.Errorf("Failed to generate sha256 checksum file: %+v", err)
}

args := []string{"start", "--download-only", "-p", profile, "--alsologtostderr", "--binary-mirror", ts.URL}

cmd := exec.CommandContext(ctx, Target(), args...)
if _, err := Run(t, cmd); err != nil {
t.Errorf("start with --binary-mirror failed %q : %v", args, err)
}
}

0 comments on commit 20d3c76

Please sign in to comment.