Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
04477912
Commit
04477912
authored
Jul 23, 2014
by
Clayton Coleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit tests for example syntax
Tests api/examples, examples, and any embedded json in readme.
parent
60aa8639
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
216 additions
and
11 deletions
+216
-11
controller-list.json
api/examples/controller-list.json
+10
-7
pod-list.json
api/examples/pod-list.json
+4
-2
service-list.json
api/examples/service-list.json
+2
-2
doc.go
examples/doc.go
+18
-0
examples_test.go
examples/examples_test.go
+182
-0
No files found.
api/examples/controller-list.json
View file @
04477912
...
@@ -9,13 +9,16 @@
...
@@ -9,13 +9,16 @@
},
},
"podTemplate"
:
{
"podTemplate"
:
{
"desiredState"
:
{
"desiredState"
:
{
"image"
:
"dockerfile/nginx"
,
"manifest"
:
{
"networkPorts"
:
[
"version"
:
"v1beta1"
,
{
"image"
:
"dockerfile/nginx"
,
"hostPort"
:
8080
,
"networkPorts"
:
[
"containerPort"
:
80
{
}
"hostPort"
:
8080
,
]
"containerPort"
:
80
}
]
}
},
},
"labels"
:
{
"labels"
:
{
"name"
:
"testRun"
"name"
:
"testRun"
...
...
api/examples/pod-list.json
View file @
04477912
...
@@ -11,12 +11,13 @@
...
@@ -11,12 +11,13 @@
"version"
:
"v1beta1"
,
"version"
:
"v1beta1"
,
"id"
:
"my-pod-1"
,
"id"
:
"my-pod-1"
,
"containers"
:
[{
"containers"
:
[{
"name"
:
"nginx"
,
"image"
:
"dockerfile/nginx"
,
"image"
:
"dockerfile/nginx"
,
"ports"
:
[{
"ports"
:
[{
"hostPort"
:
8080
,
"hostPort"
:
8080
,
"containerPort"
:
80
"containerPort"
:
80
}]
}]
}
}
]
}
}
},
},
"currentState"
:
{
"currentState"
:
{
...
@@ -34,12 +35,13 @@
...
@@ -34,12 +35,13 @@
"version"
:
"v1beta1"
,
"version"
:
"v1beta1"
,
"id"
:
"my-pod-2"
,
"id"
:
"my-pod-2"
,
"containers"
:
[{
"containers"
:
[{
"name"
:
"nginx"
,
"image"
:
"dockerfile/nginx"
,
"image"
:
"dockerfile/nginx"
,
"ports"
:
[{
"ports"
:
[{
"hostPort"
:
8080
,
"hostPort"
:
8080
,
"containerPort"
:
80
"containerPort"
:
80
}]
}]
}
}
]
}
}
},
},
"currentState"
:
{
"currentState"
:
{
...
...
api/examples/service-list.json
View file @
04477912
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
"port"
:
8000
,
"port"
:
8000
,
"labels"
:
{
"labels"
:
{
"name"
:
"nginx"
"name"
:
"nginx"
}
}
,
"selector"
:
{
"selector"
:
{
"name"
:
"nginx"
"name"
:
"nginx"
}
}
...
@@ -16,7 +16,7 @@
...
@@ -16,7 +16,7 @@
"labels"
:
{
"labels"
:
{
"env"
:
"prod"
,
"env"
:
"prod"
,
"name"
:
"jetty"
"name"
:
"jetty"
}
}
,
"selector"
:
{
"selector"
:
{
"env"
:
"prod"
,
"env"
:
"prod"
,
"name"
:
"jetty"
"name"
:
"jetty"
...
...
examples/doc.go
0 → 100644
View file @
04477912
/*
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
examples/examples_test.go
0 → 100644
View file @
04477912
/*
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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment