Commit e47f5a5b authored by bryanl's avatar bryanl
Browse files

use new ksonnet-lib generator for creating lib


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent c8642bf2
package generator
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/ksonnet"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/kubespec"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/kubeversion"
log "github.com/sirupsen/logrus"
)
var (
// ksonnetEmitter is the function which emits the ksonnet standard library.
ksonnetEmitter = ksonnet.Emit
ksonnetEmitter = ksonnet.GenerateLib
)
// KsonnetLib is the ksonnet standard library for a version of swagger.
......@@ -29,33 +28,34 @@ type KsonnetLib struct {
// Ksonnet generates the ksonnet standard library or returns an error if there was
// a problem.
func Ksonnet(swaggerData []byte) (*KsonnetLib, error) {
// Deserialize the API object.
s := kubespec.APISpec{}
if err := json.Unmarshal(swaggerData, &s); err != nil {
f, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
s.Text = swaggerData
defer os.Remove(f.Name())
// Emit Jsonnet code.
extensionsLibData, k8sLibData, err := ksonnetEmitter(&s, nil, nil)
_, err = f.Write(swaggerData)
if err != nil {
return nil, err
}
// Warn where the Kubernetes version is currently only supported as Beta.
if kubeversion.Beta(s.Info.Version) {
log.Warnf(`!
============================================================================================
Kubernetes version %s is currently supported as Beta; you may encounter unexpected behavior
============================================================================================`, s.Info.Version)
if err = f.Close(); err != nil {
return nil, err
}
spew.Dump("---", f.Name(), ksonnetEmitter)
lib, err := ksonnetEmitter(f.Name())
if err != nil {
return nil, err
}
kl := &KsonnetLib{
K: extensionsLibData,
K8s: k8sLibData,
K: lib.Extensions,
K8s: lib.K8s,
Swagger: swaggerData,
Version: s.Info.Version,
Version: lib.Version,
}
return kl, nil
......
......@@ -4,7 +4,7 @@ import (
"errors"
"testing"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/kubespec"
"github.com/ksonnet/ksonnet-lib/ksonnet-gen/ksonnet"
)
func TestKsonnet(t *testing.T) {
......@@ -16,19 +16,22 @@ func TestKsonnet(t *testing.T) {
var (
ext = []byte("k")
lib = []byte("k8s")
successfulEmit = func(*kubespec.APISpec, *string, *string) ([]byte, []byte, error) {
return ext, lib, nil
successfulEmit = func(string) (*ksonnet.Lib, error) {
return &ksonnet.Lib{
Version: "v1.7.0",
K8s: lib,
Extensions: ext,
}, nil
}
failureEmit = func(*kubespec.APISpec, *string, *string) ([]byte, []byte, error) {
return nil, nil, errors.New("failure")
failureEmit = func(string) (*ksonnet.Lib, error) {
return nil, errors.New("failure")
}
v170swagger = []byte(`{"info":{"version":"v1.7.0"}}`)
v180swagger = []byte(`{"info":{"version":"v1.8.0"}}`)
)
cases := []struct {
name string
emitter func(*kubespec.APISpec, *string, *string) ([]byte, []byte, error)
emitter func(string) (*ksonnet.Lib, error)
swaggerData []byte
version string
isErr bool
......@@ -41,6 +44,7 @@ func TestKsonnet(t *testing.T) {
},
{
name: "invalid swagger",
emitter: failureEmit,
swaggerData: []byte(`{`),
isErr: true,
},
......@@ -50,16 +54,12 @@ func TestKsonnet(t *testing.T) {
swaggerData: v170swagger,
isErr: true,
},
{
name: "valid beta swagger",
emitter: successfulEmit,
swaggerData: v180swagger,
version: "v1.8.0",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ogKSEmitter := ksonnetEmitter
defer func() { ksonnetEmitter = ogKSEmitter }()
ksonnetEmitter = tc.emitter
kl, err := Ksonnet(tc.swaggerData)
......@@ -70,7 +70,7 @@ func TestKsonnet(t *testing.T) {
}
} else {
if err != nil {
t.Fatal("Ksonnet() returned unexpected error")
t.Fatalf("Ksonnet() returned unexpected error: %#v", err)
}
if got, expected := string(kl.K), string(ext); got != expected {
......
......@@ -16,6 +16,7 @@ package lib
import (
"fmt"
"io/ioutil"
"os"
"testing"
......@@ -37,14 +38,6 @@ const (
"definitions": {
}
}`
blankK8sLib = `// AUTOGENERATED from the Kubernetes OpenAPI specification. DO NOT MODIFY.
// Kubernetes version: v1.7.0
{
local hidden = {
},
}
`
)
var testFS = afero.NewMemMapFs()
......@@ -74,7 +67,9 @@ func TestGenerateLibData(t *testing.T) {
bytes, err := afero.ReadFile(testFS, string(schemaPath))
if err != nil {
t.Fatalf("Failed to read swagger file at '%s':\n%v", schemaPath, err)
} else if actualSwagger := string(bytes); actualSwagger != blankSwaggerData {
}
if actualSwagger := string(bytes); actualSwagger != blankSwaggerData {
t.Fatalf("Expected swagger file at '%s' to have value: '%s', got: '%s'", schemaPath, blankSwaggerData, actualSwagger)
}
......@@ -82,7 +77,14 @@ func TestGenerateLibData(t *testing.T) {
k8sLibBytes, err := afero.ReadFile(testFS, string(k8sLibPath))
if err != nil {
t.Fatalf("Failed to read ksonnet-lib file at '%s':\n%v", k8sLibPath, err)
} else if actualK8sLib := string(k8sLibBytes); actualK8sLib != blankK8sLib {
}
blankK8sLib, err := ioutil.ReadFile("testdata/k8s.libsonnet")
if err != nil {
t.Fatalf("Failed to read testdata/k8s.libsonnet: %#v", err)
}
if actualK8sLib := string(k8sLibBytes); actualK8sLib != string(blankK8sLib) {
t.Fatalf("Expected swagger file at '%s' to have value: '%s', got: '%s'", k8sLibPath, blankK8sLib, actualK8sLib)
}
}
{
"__ksonnet": {
checksum: "f942f2f2b70d842504ffa21b502ad044be92110af159342a352bf1ed4c6221e2",
kubernetesVersion: "1.7.0",
},
local hidden = {
},
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment