Skip to content

Commit

Permalink
Add unit tests for example syntax
Browse files Browse the repository at this point in the history
Tests api/examples, examples, and any embedded json in readme.
  • Loading branch information
smarterclayton committed Jul 23, 2014
1 parent 60aa863 commit 0447791
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 11 deletions.
17 changes: 10 additions & 7 deletions api/examples/controller-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
},
"podTemplate": {
"desiredState": {
"image": "dockerfile/nginx",
"networkPorts": [
{
"hostPort": 8080,
"containerPort": 80
}
]
"manifest": {
"version": "v1beta1",
"image": "dockerfile/nginx",
"networkPorts": [
{
"hostPort": 8080,
"containerPort": 80
}
]
}
},
"labels": {
"name": "testRun"
Expand Down
6 changes: 4 additions & 2 deletions api/examples/pod-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
"version": "v1beta1",
"id": "my-pod-1",
"containers": [{
"name": "nginx",
"image": "dockerfile/nginx",
"ports": [{
"hostPort": 8080,
"containerPort": 80
}]
}
}]
}
},
"currentState": {
Expand All @@ -34,12 +35,13 @@
"version": "v1beta1",
"id": "my-pod-2",
"containers": [{
"name": "nginx",
"image": "dockerfile/nginx",
"ports": [{
"hostPort": 8080,
"containerPort": 80
}]
}
}]
}
},
"currentState": {
Expand Down
4 changes: 2 additions & 2 deletions api/examples/service-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"port": 8000,
"labels": {
"name": "nginx"
}
},
"selector": {
"name": "nginx"
}
Expand All @@ -16,7 +16,7 @@
"labels": {
"env": "prod",
"name": "jetty"
}
},
"selector": {
"env": "prod",
"name": "jetty"
Expand Down
18 changes: 18 additions & 0 deletions examples/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2014 Google Inc. All rights reserved.
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.
*/

// Examples contains sample applications for trying out the concepts in Kubernetes.
package examples
182 changes: 182 additions & 0 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
Copyright 2014 Google Inc. All rights reserved.
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 examples_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)

func validateObject(obj interface{}) (errors []error) {
switch t := obj.(type) {
case *api.ReplicationController:
errors = api.ValidateManifest(&t.DesiredState.PodTemplate.DesiredState.Manifest)
case *api.ReplicationControllerList:
for i := range t.Items {
errors = append(errors, validateObject(&t.Items[i])...)
}
case *api.Service:
errors = api.ValidateService(t)
case *api.ServiceList:
for i := range t.Items {
errors = append(errors, validateObject(&t.Items[i])...)
}
case *api.Pod:
errors = api.ValidateManifest(&t.DesiredState.Manifest)
case *api.PodList:
for i := range t.Items {
errors = append(errors, validateObject(&t.Items[i])...)
}
default:
return []error{fmt.Errorf("no validation defined for %#v", obj)}
}
return errors
}

func walkJSONFiles(inDir string, fn func(name, path string, data []byte)) error {
err := filepath.Walk(inDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && path != inDir {
return filepath.SkipDir
}
name := filepath.Base(path)
ext := filepath.Ext(name)
if ext != "" {
name = name[:len(name)-len(ext)]
}
if ext != ".json" {
return nil
}
glog.Infof("Testing %s", path)
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
fn(name, path, data)
return nil
})
return err
}

func TestApiExamples(t *testing.T) {
expected := map[string]interface{}{
"controller": &api.ReplicationController{},
"controller-list": &api.ReplicationControllerList{},
"pod": &api.Pod{},
"pod-list": &api.PodList{},
"service": &api.Service{},
"external-service": &api.Service{},
"service-list": &api.ServiceList{},
}

tested := 0
err := walkJSONFiles("../api/examples", func(name, path string, data []byte) {
expectedType, found := expected[name]
if !found {
t.Errorf("%s does not have a test case defined", path)
return
}
tested += 1
if err := api.DecodeInto(data, expectedType); err != nil {
t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
return
}
if errors := validateObject(expectedType); len(errors) > 0 {
t.Errorf("%s did not validate correctly: %v", path, errors)
}
})
if err != nil {
t.Errorf("Expected no error, Got %v", err)
}
if tested != len(expected) {
t.Errorf("Expected %d examples, Got %d", len(expected), tested)
}
}

func TestExamples(t *testing.T) {
expected := map[string]interface{}{
"frontend-controller": &api.ReplicationController{},
"redis-slave-controller": &api.ReplicationController{},
"redis-master": &api.Pod{},
"frontend-service": &api.Service{},
"redis-master-service": &api.Service{},
"redis-slave-service": &api.Service{},
}

tested := 0
err := walkJSONFiles("../examples/guestbook", func(name, path string, data []byte) {
expectedType, found := expected[name]
if !found {
t.Errorf("%s does not have a test case defined", path)
return
}
tested += 1
if err := api.DecodeInto(data, expectedType); err != nil {
t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
return
}
if errors := validateObject(expectedType); len(errors) > 0 {
t.Errorf("%s did not validate correctly: %v", path, errors)
}
})
if err != nil {
t.Errorf("Expected no error, Got %v", err)
}
if tested != len(expected) {
t.Errorf("Expected %d examples, Got %d", len(expected), tested)
}
}

var jsonRegexp = regexp.MustCompile("(?ms)^```\\w*\\n(\\{.+?\\})\\w*\\n^```")

func TestReadme(t *testing.T) {
path := "../README.md"
data, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("Unable to read file: %v", err)
}

match := jsonRegexp.FindStringSubmatch(string(data))
if match == nil {
return
}
for _, json := range match[1:] {
expectedType := &api.Pod{}
if err := api.DecodeInto([]byte(json), expectedType); err != nil {
t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
return
}
if errors := validateObject(expectedType); len(errors) > 0 {
t.Errorf("%s did not validate correctly: %v", path, errors)
}
encoded, err := api.Encode(expectedType)
if err != nil {
t.Errorf("Could not encode object: %v", err)
continue
}
t.Logf("Found pod %s\n%s", json, encoded)
}
}

0 comments on commit 0447791

Please sign in to comment.