Skip to content

Commit

Permalink
use go:embed to load files into test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Boudreau committed Mar 27, 2023
1 parent e126d0a commit 4606d9e
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions serviceregistration/kubernetes/testing/testserver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testing

import (
_ "embed"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -18,15 +19,27 @@ import (
const (
ExpectedNamespace = "default"
ExpectedPodName = "shell-demo"

// File names of samples pulled from real life.
caCrtFile = "ca.crt"
respGetPod = "resp-get-pod.json"
respNotFound = "resp-not-found.json"
respUpdatePod = "resp-update-pod.json"
tokenFile = "token"
)

// Pull real-life-based testing data in from files at compile time.
// We decided to embed them in the test binary because of past issues
// with reading files that we encountered on CI workers.

//go:embed ca.crt
var caCrt string

//go:embed resp-get-pod.json
var getPodResponse string

//go:embed resp-not-found.json
var notFoundResponse string

//go:embed resp-update-pod.json
var updatePodTagsResponse string

//go:embed token
var token string

var (
// ReturnGatewayTimeouts toggles whether the test server should return,
// well, gateway timeouts...
Expand Down Expand Up @@ -78,28 +91,6 @@ func Server(t *testing.T) (testState *State, testConf *Conf, closeFunc func()) {
}
}

// Read in our sample files.
token, err := readFile(tokenFile)
if err != nil {
t.Fatal(err)
}
caCrt, err := readFile(caCrtFile)
if err != nil {
t.Fatal(err)
}
notFoundResponse, err := readFile(respNotFound)
if err != nil {
t.Fatal(err)
}
getPodResponse, err := readFile(respGetPod)
if err != nil {
t.Fatal(err)
}
updatePodTagsResponse, err := readFile(respUpdatePod)
if err != nil {
t.Fatal(err)
}

// Plant our token in a place where it can be read for the config.
tmpToken, err := ioutil.TempFile("", "token")
if err != nil {
Expand Down

0 comments on commit 4606d9e

Please sign in to comment.