-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
375 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestEnvsubstIntegrationFromStdin(t *testing.T) { | ||
|
||
if os.Getenv("KUBECTL_ENVSUBST_INTEGRATION_TESTS_AVAILABLE") != "KUBECTL_ENVSUBST_INTEGRATION_TESTS_AVAILABLE" { | ||
log.Printf("Integration test was skipped due to configuration") | ||
return | ||
} | ||
|
||
resourceName := RandomIdent(32) | ||
|
||
// Setup environment variables that was used in substitution | ||
os.Setenv("IMAGE_NAME", "nginx") | ||
os.Setenv("IMAGE_TAG", "latest") | ||
os.Setenv("CI_PROJECT_NAME", resourceName) | ||
defer os.Unsetenv("IMAGE_NAME") | ||
defer os.Unsetenv("IMAGE_TAG") | ||
defer os.Unsetenv("CI_PROJECT_NAME") | ||
|
||
// configure CLI | ||
os.Setenv("ENVSUBST_ALLOWED_PREFIXES", "CI_,IMAGE_") | ||
defer os.Unsetenv("ENVSUBST_ALLOWED_PREFIXES") | ||
|
||
// Prepare input manifest | ||
manifest := ` | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: &app ${CI_PROJECT_NAME} | ||
labels: | ||
app: *app | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: *app | ||
template: | ||
metadata: | ||
labels: | ||
app: *app | ||
spec: | ||
containers: | ||
- name: *app | ||
image: $IMAGE_NAME:$IMAGE_TAG | ||
imagePullPolicy: Always | ||
` | ||
|
||
// Run kubectl-envsubst | ||
cmdEnvsubstApply := exec.Command("kubectl", "envsubst", "apply", "-f", "-") | ||
cmdEnvsubstApply.Stdin = strings.NewReader(manifest) | ||
output, err := cmdEnvsubstApply.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("Failed to run kubectl envsubst: %v, output: %s", err, string(output)) | ||
} | ||
fmt.Println(string(output)) | ||
|
||
// Check result (it should be created/updated/unchanged, etc...) | ||
expectedOutput := strings.Contains(string(output), fmt.Sprintf("deployment.apps/%s", resourceName)) | ||
if !expectedOutput { | ||
t.Errorf("Expected substituted output to contain 'deployment.apps/%s', got %s", resourceName, string(output)) | ||
} | ||
|
||
// Validate applied resource | ||
validateCmd := exec.Command("kubectl", "get", "deployment", resourceName) | ||
validateOutput, err := validateCmd.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("Failed to validate applied resource: %v, output: %s", err, string(validateOutput)) | ||
} | ||
if !strings.Contains(string(validateOutput), resourceName) { | ||
t.Errorf("Expected deployment 'kubectl-envsubst-integration-test' to exist, got %s", string(validateOutput)) | ||
} | ||
|
||
// cleanup | ||
cmdDelete := exec.Command("kubectl", "delete", "deploy", resourceName) | ||
outputDel, err := cmdDelete.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("Failed to cleanup: %v, output: %s", err, string(output)) | ||
} | ||
fmt.Println(string(outputDel)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package integration | ||
|
||
import ( | ||
"math/rand" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func RandomIdent(length int) string { | ||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
seededRand := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
b := make([]byte, length) | ||
for i := range b { | ||
b[i] = charset[seededRand.Intn(len(charset))] | ||
} | ||
return strings.ToLower("I" + string(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestReadRemote(t *testing.T) { | ||
|
||
t.Run("Successful Request", func(t *testing.T) { | ||
mockHTTPResponse := "Remote file content" | ||
http.DefaultClient = &http.Client{ | ||
Transport: roundTripper(func(req *http.Request) *http.Response { | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(strings.NewReader(mockHTTPResponse)), | ||
} | ||
}), | ||
} | ||
result, err := readRemote("http://example.com/data") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
if string(result) != mockHTTPResponse { | ||
t.Errorf("Expected: %s, Got: %s", mockHTTPResponse, string(result)) | ||
} | ||
}) | ||
|
||
t.Run("Failed Request", func(t *testing.T) { | ||
http.DefaultClient = &http.Client{ | ||
Transport: roundTripper(func(req *http.Request) *http.Response { | ||
return &http.Response{ | ||
StatusCode: 404, | ||
Body: io.NopCloser(strings.NewReader("Not Found")), | ||
} | ||
}), | ||
} | ||
_, err := readRemote("http://example.com/not-found") | ||
if err == nil { | ||
t.Error("Expected error but got none") | ||
} | ||
}) | ||
} | ||
|
||
// Helper for mocking http.Client | ||
type roundTripper func(req *http.Request) *http.Response | ||
|
||
func (f roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return f(req), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.