2014-07-23 18:08:16 +00:00
|
|
|
/*
|
|
|
|
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"
|
2015-03-13 22:19:55 +00:00
|
|
|
"strings"
|
2014-07-23 18:08:16 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-09-01 05:10:49 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-03-17 03:43:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/yaml"
|
2014-07-23 18:08:16 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2014-09-06 02:22:03 +00:00
|
|
|
func validateObject(obj runtime.Object) (errors []error) {
|
2014-09-29 21:38:25 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-07-23 18:08:16 +00:00
|
|
|
switch t := obj.(type) {
|
|
|
|
case *api.ReplicationController:
|
2014-11-07 02:09:46 +00:00
|
|
|
if t.Namespace == "" {
|
|
|
|
t.Namespace = api.NamespaceDefault
|
|
|
|
}
|
|
|
|
errors = validation.ValidateReplicationController(t)
|
2014-07-23 18:08:16 +00:00
|
|
|
case *api.ReplicationControllerList:
|
|
|
|
for i := range t.Items {
|
|
|
|
errors = append(errors, validateObject(&t.Items[i])...)
|
|
|
|
}
|
|
|
|
case *api.Service:
|
2014-11-07 02:09:46 +00:00
|
|
|
if t.Namespace == "" {
|
|
|
|
t.Namespace = api.NamespaceDefault
|
|
|
|
}
|
2014-10-23 20:51:34 +00:00
|
|
|
api.ValidNamespace(ctx, &t.ObjectMeta)
|
2015-01-27 23:55:54 +00:00
|
|
|
errors = validation.ValidateService(t)
|
2014-07-23 18:08:16 +00:00
|
|
|
case *api.ServiceList:
|
|
|
|
for i := range t.Items {
|
|
|
|
errors = append(errors, validateObject(&t.Items[i])...)
|
|
|
|
}
|
|
|
|
case *api.Pod:
|
2014-11-07 02:09:46 +00:00
|
|
|
if t.Namespace == "" {
|
|
|
|
t.Namespace = api.NamespaceDefault
|
|
|
|
}
|
2014-10-23 20:51:34 +00:00
|
|
|
api.ValidNamespace(ctx, &t.ObjectMeta)
|
2014-11-07 02:09:46 +00:00
|
|
|
errors = validation.ValidatePod(t)
|
2014-07-23 18:08:16 +00:00
|
|
|
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 {
|
2015-03-13 22:19:55 +00:00
|
|
|
return filepath.Walk(inDir, func(path string, info os.FileInfo, err error) error {
|
2014-07-23 18:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-13 22:19:55 +00:00
|
|
|
|
2014-07-23 18:08:16 +00:00
|
|
|
if info.IsDir() && path != inDir {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2015-03-13 22:19:55 +00:00
|
|
|
|
|
|
|
file := filepath.Base(path)
|
|
|
|
if ext := filepath.Ext(file); ext == ".json" || ext == ".yaml" {
|
|
|
|
glog.Infof("Testing %s", path)
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := strings.TrimSuffix(file, ext)
|
2015-03-17 03:43:59 +00:00
|
|
|
|
|
|
|
if ext == ".yaml" {
|
|
|
|
out, err := yaml.ToJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data = out
|
|
|
|
}
|
|
|
|
|
2015-03-13 22:19:55 +00:00
|
|
|
fn(name, path, data)
|
2014-07-23 18:08:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-19 17:14:33 +00:00
|
|
|
func TestExampleObjectSchemas(t *testing.T) {
|
|
|
|
cases := map[string]map[string]runtime.Object{
|
2015-03-09 21:44:14 +00:00
|
|
|
"../docs/getting-started-guides": {
|
|
|
|
"pod": &api.Pod{},
|
|
|
|
},
|
|
|
|
"../cmd/integration": {
|
2015-03-18 08:21:10 +00:00
|
|
|
"v1beta1-controller": &api.ReplicationController{},
|
|
|
|
"v1beta3-controller": &api.ReplicationController{},
|
2014-09-19 17:14:33 +00:00
|
|
|
},
|
|
|
|
"../examples/guestbook": {
|
2015-02-05 23:20:27 +00:00
|
|
|
"frontend-controller": &api.ReplicationController{},
|
|
|
|
"redis-slave-controller": &api.ReplicationController{},
|
|
|
|
"redis-master-controller": &api.ReplicationController{},
|
|
|
|
"frontend-service": &api.Service{},
|
|
|
|
"redis-master-service": &api.Service{},
|
|
|
|
"redis-slave-service": &api.Service{},
|
2014-09-19 17:14:33 +00:00
|
|
|
},
|
2015-02-23 21:47:22 +00:00
|
|
|
"../examples/guestbook/v1beta3": {
|
2015-02-19 15:46:59 +00:00
|
|
|
"frontend-controller": &api.ReplicationController{},
|
|
|
|
"redis-slave-controller": &api.ReplicationController{},
|
|
|
|
"redis-master": &api.ReplicationController{},
|
|
|
|
"frontend-service": &api.Service{},
|
|
|
|
"redis-master-service": &api.Service{},
|
|
|
|
"redis-slave-service": &api.Service{},
|
|
|
|
},
|
2015-02-23 21:30:36 +00:00
|
|
|
"../examples/guestbook-go": {
|
|
|
|
"guestbook-controller": &api.ReplicationController{},
|
|
|
|
"redis-slave-controller": &api.ReplicationController{},
|
|
|
|
"redis-master-controller": &api.ReplicationController{},
|
|
|
|
"guestbook-service": &api.Service{},
|
|
|
|
"redis-master-service": &api.Service{},
|
|
|
|
"redis-slave-service": &api.Service{},
|
|
|
|
},
|
2015-02-23 21:47:22 +00:00
|
|
|
"../examples/guestbook-go/v1beta3": {
|
2015-02-23 21:30:36 +00:00
|
|
|
"guestbook-controller": &api.ReplicationController{},
|
|
|
|
"redis-slave-controller": &api.ReplicationController{},
|
|
|
|
"redis-master-controller": &api.ReplicationController{},
|
|
|
|
"guestbook-service": &api.Service{},
|
|
|
|
"redis-master-service": &api.Service{},
|
|
|
|
"redis-slave-service": &api.Service{},
|
|
|
|
},
|
2014-09-19 17:14:33 +00:00
|
|
|
"../examples/walkthrough": {
|
|
|
|
"pod1": &api.Pod{},
|
|
|
|
"pod2": &api.Pod{},
|
2014-10-21 14:19:55 +00:00
|
|
|
"pod-with-http-healthcheck": &api.Pod{},
|
|
|
|
"service": &api.Service{},
|
|
|
|
"replication-controller": &api.ReplicationController{},
|
2014-09-19 17:14:33 +00:00
|
|
|
},
|
2015-03-04 21:02:47 +00:00
|
|
|
"../examples/update-demo/v1beta1": {
|
|
|
|
"kitten-rc": &api.ReplicationController{},
|
|
|
|
"nautilus-rc": &api.ReplicationController{},
|
|
|
|
},
|
|
|
|
"../examples/update-demo/v1beta3": {
|
2015-01-16 21:22:55 +00:00
|
|
|
"kitten-rc": &api.ReplicationController{},
|
|
|
|
"nautilus-rc": &api.ReplicationController{},
|
|
|
|
},
|
2014-07-23 18:08:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 17:14:33 +00:00
|
|
|
for path, expected := range cases {
|
|
|
|
tested := 0
|
|
|
|
err := walkJSONFiles(path, 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 := latest.Codec.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)
|
2014-07-23 18:08:16 +00:00
|
|
|
}
|
2014-09-19 17:14:33 +00:00
|
|
|
if tested != len(expected) {
|
|
|
|
t.Errorf("Expected %d examples, Got %d", len(expected), tested)
|
2014-07-23 18:08:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 17:14:33 +00:00
|
|
|
var sampleRegexp = regexp.MustCompile("(?ms)^```(?:(?P<type>yaml)\\w*\\n(?P<content>.+?)|\\w*\\n(?P<content>\\{.+?\\}))\\w*\\n^```")
|
|
|
|
var subsetRegexp = regexp.MustCompile("(?ms)\\.{3}")
|
2014-07-23 18:08:16 +00:00
|
|
|
|
|
|
|
func TestReadme(t *testing.T) {
|
2014-09-19 17:14:33 +00:00
|
|
|
paths := []string{
|
|
|
|
"../README.md",
|
|
|
|
"../examples/walkthrough/README.md",
|
2014-07-23 18:08:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 17:14:33 +00:00
|
|
|
for _, path := range paths {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
2014-07-23 18:08:16 +00:00
|
|
|
if err != nil {
|
2014-09-19 17:14:33 +00:00
|
|
|
t.Errorf("Unable to read file %s: %v", path, err)
|
2014-07-23 18:08:16 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-09-19 17:14:33 +00:00
|
|
|
|
|
|
|
matches := sampleRegexp.FindAllStringSubmatch(string(data), -1)
|
|
|
|
if matches == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, match := range matches {
|
|
|
|
var content, subtype string
|
|
|
|
for i, name := range sampleRegexp.SubexpNames() {
|
|
|
|
if name == "type" {
|
|
|
|
subtype = match[i]
|
|
|
|
}
|
|
|
|
if name == "content" && match[i] != "" {
|
|
|
|
content = match[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if subtype == "yaml" && subsetRegexp.FindString(content) != "" {
|
|
|
|
t.Logf("skipping (%s): \n%s", subtype, content)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//t.Logf("testing (%s): \n%s", subtype, content)
|
|
|
|
expectedType := &api.Pod{}
|
2015-03-17 03:43:59 +00:00
|
|
|
json, err := yaml.ToJSON([]byte(content))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s could not be converted to JSON: %v\n%s", path, err, string(content))
|
|
|
|
}
|
|
|
|
if err := latest.Codec.DecodeInto(json, expectedType); err != nil {
|
2014-09-19 17:14:33 +00:00
|
|
|
t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(content))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if errors := validateObject(expectedType); len(errors) > 0 {
|
|
|
|
t.Errorf("%s did not validate correctly: %v", path, errors)
|
|
|
|
}
|
2015-03-17 03:43:59 +00:00
|
|
|
_, err = latest.Codec.Encode(expectedType)
|
2014-09-19 17:14:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not encode object: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 18:08:16 +00:00
|
|
|
}
|
|
|
|
}
|