diff --git a/Dockerfile.bats b/Dockerfile.bats index ec78373..18a545d 100644 --- a/Dockerfile.bats +++ b/Dockerfile.bats @@ -1,5 +1,5 @@ FROM bats/bats:v1.2.1 -RUN apk --no-cache add ca-certificates parallel +RUN apk --no-cache add ca-certificates parallel python COPY bin/kubeconform /code/bin/ COPY acceptance.bats /code/acceptance.bats COPY fixtures /code/fixtures diff --git a/acceptance.bats b/acceptance.bats index 196bd46..a7805a5 100755 --- a/acceptance.bats +++ b/acceptance.bats @@ -1,5 +1,36 @@ #!/usr/bin/env bats +cp /etc/hosts /etc/hosts.original +cp /etc/resolv.conf /etc/resolv.conf.original + +teardown() { + cat /etc/resolv.conf.original > /etc/resolv.conf + cat /etc/hosts.original > /etc/hosts +} + +blockNetwork() { + echo "nameserver 127.0.0.1" > /etc/resolv.conf +} + +startRegistryHTTPServer() { + /bin/sh -c ' + # Start simple static server for schemas + cd fixtures/registry/ + python -m SimpleHTTPServer 3000 2>/dev/null 1>/dev/null & + echo -n $! > /tmp/registry-pid + + # Wait until server is ready + until wget http://localhost:3000/trainingjob-sagemaker-v1.json -qO /dev/null 2>/dev/null + do + echo -n "" + done + ' 3>/dev/null +} + +stopRegistryHTTPServer() { + kill "$(cat /tmp/registry-pid)" +} + @test "Pass when parsing a valid Kubernetes config YAML file" { run bin/kubeconform -summary fixtures/valid.yaml [ "$status" -eq 0 ] @@ -204,13 +235,29 @@ } @test "Pass when parsing a valid Kubernetes config YAML file and store cache" { - run mkdir cache + run rm -rf cache && mkdir cache run bin/kubeconform -cache cache -summary fixtures/valid.yaml [ "$status" -eq 0 ] [ "$output" = "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ] [ "`ls cache/ | wc -l`" -eq 1 ] } +@test "Pass when parsing a valid Kubernetes config YAML file offline using cache" { + startRegistryHTTPServer + + run rm -rf cache && mkdir cache + run bin/kubeconform -cache cache -schema-location 'http://localhost:3000/{{ .ResourceKind }}{{ .KindSuffix }}.json' -summary fixtures/test_crd.yaml + [ "$status" -eq 0 ] + [ "$output" = "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ] + + stopRegistryHTTPServer + blockNetwork + + run bin/kubeconform -cache cache -schema-location 'http://localhost:3000/{{ .ResourceKind }}{{ .KindSuffix }}.json' -summary fixtures/test_crd.yaml + [ "$status" -eq 0 ] + [ "$output" = "Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ] +} + @test "Fail when cache folder does not exist" { run bin/kubeconform -cache cache_does_not_exist -summary fixtures/valid.yaml [ "$status" -eq 1 ] diff --git a/fixtures/registry/trainingjob-sagemaker-v1.json b/fixtures/registry/trainingjob-sagemaker-v1.json index dc58bbb..8fc1e39 100644 --- a/fixtures/registry/trainingjob-sagemaker-v1.json +++ b/fixtures/registry/trainingjob-sagemaker-v1.json @@ -10,7 +10,8 @@ "type": "string" }, "metadata": { - "type": "object" + "$ref": "https://kubernetesjsonschema.dev/v1.14.0/_definitions.json#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta", + "description": "Standard object metadata." }, "spec": { "description": "TrainingJobSpec defines the desired state of TrainingJob",