-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract troubleshoot specs from secrets/config maps (#175)
- Loading branch information
Showing
18 changed files
with
481 additions
and
19 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
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,16 @@ | ||
package handlers | ||
|
||
import ( | ||
"embed" | ||
|
||
kjs "github.com/replicatedhq/kots-lint/kubernetes_json_schema" | ||
"github.com/replicatedhq/kots-lint/pkg/kots" | ||
) | ||
|
||
func init() { | ||
kjs.KubernetesJsonSchemaDir = "../../kubernetes_json_schema/schema" | ||
kots.InitOPALinting() | ||
} | ||
|
||
//go:embed test-data/* | ||
var testdata embed.FS |
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,220 @@ | ||
package handlers | ||
|
||
import ( | ||
"archive/tar" | ||
"encoding/base64" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/replicatedhq/kots-lint/pkg/kots" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/stretchr/testify.v1/assert" | ||
) | ||
|
||
func Test_LintRelease(t *testing.T) { | ||
|
||
type resultType struct { | ||
LintExpressions []kots.LintExpression `json:"lintExpressions"` | ||
} | ||
|
||
getTarReader := func(filesNames []string) io.Reader { | ||
pipeReader, pipeWriter := io.Pipe() | ||
|
||
go func() { | ||
defer pipeWriter.Close() | ||
|
||
tarWriter := tar.NewWriter(pipeWriter) | ||
defer tarWriter.Close() | ||
|
||
for _, fileName := range filesNames { | ||
data, err := testdata.ReadFile(fileName) | ||
if err != nil { | ||
t.Fatalf("failed to open file: %v", err) | ||
} | ||
|
||
if strings.HasSuffix(fileName, ".tgz") { | ||
data = []byte(base64.StdEncoding.EncodeToString(data)) | ||
} | ||
|
||
header := &tar.Header{ | ||
Name: filepath.Base(fileName), | ||
Mode: 0644, | ||
Size: int64(len(data)), | ||
} | ||
|
||
tarWriter.WriteHeader(header) | ||
tarWriter.Write(data) | ||
} | ||
}() | ||
|
||
return pipeReader | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
chartReader func(t *testing.T) io.ReadCloser | ||
contentType string | ||
want resultType | ||
}{ | ||
{ | ||
name: "one valid chart without kotskinds", | ||
chartReader: func(t *testing.T) io.ReadCloser { | ||
yamlFiles, err := testdata.ReadDir("test-data/kots/without-preflight") | ||
assert.NoError(t, err) | ||
|
||
files := []string{ | ||
"test-data/builders/testchart-with-labels-16.2.2.tgz", | ||
} | ||
for _, f := range yamlFiles { | ||
files = append(files, filepath.Join("test-data/kots/without-preflight", f.Name())) | ||
} | ||
|
||
return io.NopCloser(getTarReader(files)) | ||
}, | ||
contentType: "application/tar", | ||
want: resultType{ | ||
LintExpressions: []kots.LintExpression{ | ||
{ | ||
Rule: "preflight-spec", | ||
Type: "warn", | ||
Message: "Missing preflight spec", | ||
Path: "", | ||
Positions: nil, | ||
}, | ||
{ | ||
Rule: "application-spec", | ||
Type: "warn", | ||
Message: "Missing application spec", | ||
Path: "", | ||
Positions: nil, | ||
}, | ||
{ | ||
Rule: "config-spec", | ||
Type: "warn", | ||
Message: "Missing config spec", | ||
Path: "", | ||
Positions: nil, | ||
}, | ||
{ | ||
Rule: "troubleshoot-spec", | ||
Type: "warn", | ||
Message: "Missing troubleshoot spec", | ||
Path: "", | ||
Positions: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "one valid chart without kotskinds but with preflights", | ||
chartReader: func(t *testing.T) io.ReadCloser { | ||
yamlFiles, err := testdata.ReadDir("test-data/kots/with-preflight") | ||
assert.NoError(t, err) | ||
|
||
files := []string{ | ||
"test-data/builders/testchart-with-labels-with-preflightspec-in-secret-16.2.2.tgz", | ||
} | ||
for _, f := range yamlFiles { | ||
files = append(files, filepath.Join("test-data/kots/with-preflight", f.Name())) | ||
} | ||
|
||
return io.NopCloser(getTarReader(files)) | ||
}, | ||
contentType: "application/tar", | ||
want: resultType{ | ||
LintExpressions: []kots.LintExpression{ | ||
{ | ||
Rule: "application-spec", | ||
Type: "warn", | ||
Message: "Missing application spec", | ||
Path: "", | ||
Positions: nil, | ||
}, | ||
{ | ||
Rule: "config-spec", | ||
Type: "warn", | ||
Message: "Missing config spec", | ||
Path: "", | ||
Positions: nil, | ||
}, | ||
{ | ||
Rule: "troubleshoot-spec", | ||
Type: "warn", | ||
Message: "Missing troubleshoot spec", | ||
Path: "", | ||
Positions: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "one valid chart with kotskinds but without preflights", | ||
chartReader: func(t *testing.T) io.ReadCloser { | ||
yamlFiles, err := testdata.ReadDir("test-data/kots/with-kots-kinds") | ||
assert.NoError(t, err) | ||
|
||
files := []string{ | ||
"test-data/builders/testchart-with-labels-16.2.2.tgz", | ||
} | ||
for _, f := range yamlFiles { | ||
files = append(files, filepath.Join("test-data/kots/with-kots-kinds", f.Name())) | ||
} | ||
|
||
return io.NopCloser(getTarReader(files)) | ||
}, | ||
contentType: "application/tar", | ||
want: resultType{ | ||
LintExpressions: []kots.LintExpression{ | ||
{ | ||
Rule: "application-statusInformers", | ||
Type: "warn", | ||
Message: "Missing application statusInformers", | ||
Path: "kots-app.yaml", | ||
Positions: []kots.LintExpressionItemPosition{ | ||
{ | ||
Start: kots.LintExpressionItemLinePosition{ | ||
Line: 5, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := require.New(t) | ||
|
||
clientRequest := &http.Request{ | ||
Body: tt.chartReader(t), | ||
Header: http.Header{ | ||
"Content-Type": []string{tt.contentType}, | ||
}, | ||
} | ||
respWriter := httptest.NewRecorder() | ||
|
||
c, _ := gin.CreateTestContext(respWriter) | ||
c.Request = clientRequest | ||
|
||
LintRelease(c) | ||
|
||
req.Equal(http.StatusOK, respWriter.Result().StatusCode) | ||
|
||
body, err := io.ReadAll(respWriter.Body) | ||
req.NoError(err) | ||
|
||
var got resultType | ||
err = json.Unmarshal(body, &got) | ||
req.NoError(err) | ||
req.ElementsMatch(tt.want.LintExpressions, got.LintExpressions) | ||
}) | ||
} | ||
} |
Binary file added
BIN
+834 Bytes
...handlers/test-data/builders/testchart-with-labels-with-preflightspec-in-secret-16.2.2.tgz
Binary file not shown.
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,8 @@ | ||
apiVersion: kots.io/v1beta2 | ||
kind: HelmChart | ||
metadata: | ||
name: testchart-with-labels | ||
spec: | ||
chart: | ||
name: testchart-with-labels | ||
chartVersion: 16.2.2 |
Oops, something went wrong.