Files
kubernetes/pkg/kubecfg/parse_test.go
Clayton Coleman 61e3ce7ddc Make runtime less global for Codec
* Make Codec separate from Scheme
* Move EncodeOrDie off Scheme to take a Codec
* Make Copy work without a Codec
* Create a "latest" package that imports all versions and
  sets global defaults for "most recent encoding"
  * v1beta1 is the current "latest", v1beta2 exists
  * Kill DefaultCodec, replace it with "latest.Codec"
  * This updates the client and etcd to store the latest known version
* EmbeddedObject is per schema and per package now
* Move runtime.DefaultScheme to api.Scheme
* Split out WatchEvent since it's not an API object today, treat it
like a special object in api
* Kill DefaultResourceVersioner, instead place it on "latest" (as the
  package that understands all packages)
* Move objDiff to runtime.ObjectDiff
2014-09-16 16:26:43 -04:00

141 lines
4.0 KiB
Go

/*
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 kubecfg
import (
"encoding/json"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"gopkg.in/v1/yaml"
)
func TestParseBadStorage(t *testing.T) {
p := NewParser(map[string]runtime.Object{})
_, err := p.ToWireFormat([]byte("{}"), "badstorage", latest.Codec)
if err == nil {
t.Errorf("Expected error, received none")
}
}
func DoParseTest(t *testing.T, storage string, obj runtime.Object, p *Parser) {
jsonData, _ := latest.Codec.Encode(obj)
var tmp map[string]interface{}
json.Unmarshal(jsonData, &tmp)
yamlData, _ := yaml.Marshal(tmp)
t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
t.Logf("Intermediate json:\n%v\n", string(jsonData))
jsonGot, jsonErr := p.ToWireFormat(jsonData, storage, latest.Codec)
yamlGot, yamlErr := p.ToWireFormat(yamlData, storage, latest.Codec)
if jsonErr != nil {
t.Errorf("json err: %#v", jsonErr)
}
if yamlErr != nil {
t.Errorf("yaml err: %#v", yamlErr)
}
if string(jsonGot) != string(jsonData) {
t.Errorf("json output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
string(jsonGot), string(jsonData))
}
if string(yamlGot) != string(jsonData) {
t.Errorf("yaml parsed output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
string(yamlGot), string(jsonData))
}
}
var testParser = NewParser(map[string]runtime.Object{
"pods": &api.Pod{},
"services": &api.Service{},
"replicationControllers": &api.ReplicationController{},
})
func TestParsePod(t *testing.T) {
DoParseTest(t, "pods", &api.Pod{
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "test pod", Kind: "Pod"},
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
ID: "My manifest",
Containers: []api.Container{
{Name: "my container"},
},
Volumes: []api.Volume{
{Name: "volume"},
},
},
},
}, testParser)
}
func TestParseService(t *testing.T) {
DoParseTest(t, "services", &api.Service{
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "my service", Kind: "Service"},
Port: 8080,
Labels: map[string]string{
"area": "staging",
},
Selector: map[string]string{
"area": "staging",
},
}, testParser)
}
func TestParseController(t *testing.T) {
DoParseTest(t, "replicationControllers", &api.ReplicationController{
JSONBase: api.JSONBase{APIVersion: "v1beta1", ID: "my controller", Kind: "ReplicationController"},
DesiredState: api.ReplicationControllerState{
Replicas: 9001,
PodTemplate: api.PodTemplate{
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
ID: "My manifest",
Containers: []api.Container{
{Name: "my container"},
},
Volumes: []api.Volume{
{Name: "volume"},
},
},
},
},
},
}, testParser)
}
type TestParseType struct {
api.JSONBase `json:",inline" yaml:",inline"`
Data string `json:"data" yaml:"data"`
}
func (*TestParseType) IsAnAPIObject() {}
func TestParseCustomType(t *testing.T) {
api.Scheme.AddKnownTypes("", &TestParseType{})
api.Scheme.AddKnownTypes("v1beta1", &TestParseType{})
api.Scheme.AddKnownTypes("v1beta2", &TestParseType{})
parser := NewParser(map[string]runtime.Object{
"custom": &TestParseType{},
})
DoParseTest(t, "custom", &TestParseType{
JSONBase: api.JSONBase{APIVersion: "", ID: "my custom object", Kind: "TestParseType"},
Data: "test data",
}, parser)
}