Skip to content

Commit

Permalink
Extract UserData from CloudFormation output during testing
Browse files Browse the repository at this point in the history
This gives us some sanity, so we can peek inside the base64 blob
  • Loading branch information
justinsb committed Aug 29, 2017
1 parent 327235a commit e793562
Show file tree
Hide file tree
Showing 5 changed files with 691 additions and 4 deletions.
59 changes: 57 additions & 2 deletions cmd/kops/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"io"
"io/ioutil"
"os"
"path"
Expand All @@ -31,11 +34,13 @@ import (
"testing"
"time"

"golang.org/x/crypto/ssh"

"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/diff"
"k8s.io/kops/pkg/jsonutils"
"k8s.io/kops/pkg/testutils"

"github.com/ghodss/yaml"
"golang.org/x/crypto/ssh"
)

// TestMinimal runs the test on a minimum configuration, similar to kops create cluster minimal.example.com --zones us-west-1a
Expand Down Expand Up @@ -335,6 +340,39 @@ func runTestCloudformation(t *testing.T, clusterName string, srcDir string, vers
t.Fatalf("unexpected error reading expected cloudformation output: %v", err)
}

// Expand out the UserData base64 blob, as otherwise testing is painful
extracted := make(map[string]string)
var buf bytes.Buffer
out := jsonutils.NewJSONStreamWriter(&buf)
in := json.NewDecoder(bytes.NewReader(actualCF))
for {
token, err := in.Token()
if err != nil {
if err == io.EOF {
break
} else {
t.Fatalf("unexpected error parsing cloudformation output: %v", err)
}
}

if strings.HasSuffix(out.Path(), ".UserData") {
if s, ok := token.(string); ok {
vBytes, err := base64.StdEncoding.DecodeString(s)
if err != nil {
t.Fatalf("error decoding UserData: %v", err)
} else {
extracted[out.Path()] = string(vBytes)
token = json.Token("extracted")
}
}
}

if err := out.WriteToken(token); err != nil {
t.Fatalf("error writing json: %v", err)
}
}
actualCF = buf.Bytes()

expectedCFTrimmed := strings.TrimSpace(string(expectedCF))
actualCFTrimmed := strings.TrimSpace(string(actualCF))
if actualCFTrimmed != expectedCFTrimmed {
Expand All @@ -349,6 +387,23 @@ func runTestCloudformation(t *testing.T, clusterName string, srcDir string, vers

t.Fatalf("cloudformation output differed from expected")
}

actualExtracted, err := yaml.Marshal(extracted)
if err != nil {
t.Fatalf("unexpected error serializing extracted values: %v", err)
}
expectedExtracted, err := ioutil.ReadFile(path.Join(srcDir, expectedCfPath+".extracted.yaml"))
if err != nil {
t.Fatalf("unexpected error reading expected extracted cloudformation output: %v", err)
}

actualExtractedTrimmed := strings.TrimSpace(string(actualExtracted))
expectedExtractedTrimmed := strings.TrimSpace(string(expectedExtracted))
if actualExtractedTrimmed != expectedExtractedTrimmed {
diffString := diff.FormatDiff(actualExtractedTrimmed, expectedExtractedTrimmed)
t.Logf("diff:\n%s\n", diffString)
t.Fatalf("cloudformation output differed from expected")
}
}
}

Expand Down
1 change: 1 addition & 0 deletions hack/.packages
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ k8s.io/kops/pkg/edit
k8s.io/kops/pkg/featureflag
k8s.io/kops/pkg/flagbuilder
k8s.io/kops/pkg/instancegroups
k8s.io/kops/pkg/jsonutils
k8s.io/kops/pkg/kubeconfig
k8s.io/kops/pkg/kubemanifest
k8s.io/kops/pkg/model
Expand Down
184 changes: 184 additions & 0 deletions pkg/jsonutils/streamwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package jsonutils

import (
"encoding/json"
"fmt"
"io"
"strings"
)

// JSONStreamWriter writes tokens as parsed by a json.Decoder back to a string
type JSONStreamWriter struct {
// out is the output destination
out io.Writer

// indent is the current indent level
indent string

// state stores a stack of the json state, comprised of [ { and F characters. F=field
state string

// deferred is used to buffer the output temporarily, used to prevent a trailing comma in an object
deferred string

// path is the current stack of fields, used to support the Path() function
path []string
}

// NewJSONStreamWriter is the constructor for a JSONStreamWriter
func NewJSONStreamWriter(out io.Writer) *JSONStreamWriter {
return &JSONStreamWriter{
out: out,
}
}

// Path returns the path to the current position in the JSON tree
func (j *JSONStreamWriter) Path() string {
return strings.Join(j.path, ".")
}

// WriteToken writes the next token to the output
func (j *JSONStreamWriter) WriteToken(token json.Token) error {
state := byte(0)
if j.state != "" {
state = j.state[len(j.state)-1]
}

var v string
switch tt := token.(type) {
// Delim, for the four JSON delimiters [ ] { }
case json.Delim:
v = tt.String()
indent := j.indent
switch tt {
case json.Delim('{'):
j.indent += " "
j.state += "{"
case json.Delim('['):
j.indent += " "
j.state += "["
case json.Delim(']'), json.Delim('}'):
j.indent = j.indent[:len(j.indent)-2]
indent = j.indent
j.state = j.state[:len(j.state)-1]
if j.state != "" && j.state[len(j.state)-1] == 'F' {
j.state = j.state[:len(j.state)-1]
j.path = j.path[:len(j.path)-1]
}
// Don't put a comma on the last field in a block
if j.deferred == ",\n" {
j.deferred = "\n"
}
default:
return fmt.Errorf("unknown delim: %v", tt)
}

switch state {
case 0:
if err := j.writeRaw(indent + v); err != nil {
return err
}
case '{':
if err := j.writeRaw(indent + v); err != nil {
return err
}
case '[':
if err := j.writeRaw(indent + v); err != nil {
return err
}
case 'F':
if err := j.writeRaw(v); err != nil {
return err
}

default:
return fmt.Errorf("unhandled state for json delim serialization: %v %q", state, j.state)
}

switch tt {
case json.Delim('{'):
j.deferred = "\n"
case json.Delim('['):
j.deferred = "\n"
case json.Delim(']'), json.Delim('}'):
j.deferred = ",\n"
default:
return fmt.Errorf("unknown delim: %v", tt)
}

return nil

// bool, for JSON booleans
case bool:
v = fmt.Sprintf("%v", tt)

// string, for JSON string literals
case string:
v = "\"" + tt + "\""

// float64, for JSON numbers
case float64:
v = fmt.Sprintf("%g", tt)

// Number, for JSON numbers
case json.Number:
v = tt.String()

// nil, for JSON null
case nil:
v = "null"

default:
return fmt.Errorf("unhandled token type %T", tt)
}

switch state {
case '{':
j.state += "F"
j.path = append(j.path, fmt.Sprintf("%s", token))
return j.writeRaw(j.indent + v + ": ")
case '[':
if err := j.writeRaw(j.indent + v); err != nil {
return err
}
j.deferred = ",\n"
return nil
case 'F':
j.state = j.state[:len(j.state)-1]
j.path = j.path[:len(j.path)-1]
if err := j.writeRaw(v); err != nil {
return err
}
j.deferred = ",\n"
return nil
}

return fmt.Errorf("unhandled state for json value (%T %q) serialization: %v %q", token, v, state, j.state)
}

func (j *JSONStreamWriter) writeRaw(s string) error {
if j.deferred != "" {
if _, err := j.out.Write([]byte(j.deferred)); err != nil {
return err
}
j.deferred = ""
}
_, err := j.out.Write([]byte(s))
return err
}
Loading

0 comments on commit e793562

Please sign in to comment.