Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Ijaz Ahmad
ksonnet
Commits
7b6c9a54
Unverified
Commit
7b6c9a54
authored
Mar 28, 2018
by
bryanl
Browse files
introduce method to test cmd flags
Signed-off-by:
bryanl
<
bryanliles@gmail.com
>
parent
9c4279da
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
123 additions
and
33 deletions
+123
-33
actions/init_test.go
actions/init_test.go
+1
-1
client/client.go
client/client.go
+20
-12
cmd/actions.go
cmd/actions.go
+30
-0
cmd/env.go
cmd/env.go
+8
-4
cmd/init.go
cmd/init.go
+12
-9
cmd/init_test.go
cmd/init_test.go
+46
-1
pkg/appinit/init_test.go
pkg/appinit/init_test.go
+5
-5
pkg/kubecfg/validate.go
pkg/kubecfg/validate.go
+1
-1
No files found.
actions/init_test.go
View file @
7b6c9a54
...
...
@@ -33,7 +33,7 @@ func TestInit(t *testing.T) {
aRootPath
:=
appMock
.
Root
()
aK8sSpecFlag
:=
"specFlag"
aServerURI
:=
"http://example.com"
aNamespace
:=
"
default
"
aNamespace
:=
"
my-namespace
"
a
,
err
:=
NewInit
(
aFs
,
aName
,
aRootPath
,
aK8sSpecFlag
,
aServerURI
,
aNamespace
)
require
.
NoError
(
t
,
err
)
...
...
client/client.go
View file @
7b6c9a54
...
...
@@ -287,19 +287,27 @@ func (c *Config) overrideCluster(envName string) error {
return
err
}
if
_
,
ok
:=
servers
[
server
];
ok
{
clusterName
:=
servers
[
server
]
if
c
.
Overrides
.
Context
.
Cluster
==
""
{
log
.
Debugf
(
"Overwriting --cluster flag with '%s'"
,
clusterName
)
c
.
Overrides
.
Context
.
Cluster
=
clusterName
if
len
(
servers
)
>
0
{
if
_
,
ok
:=
servers
[
server
];
ok
{
clusterName
:=
servers
[
server
]
if
c
.
Overrides
.
Context
.
Cluster
==
""
{
log
.
Debugf
(
"Overwriting --cluster flag with '%s'"
,
clusterName
)
c
.
Overrides
.
Context
.
Cluster
=
clusterName
}
if
c
.
Overrides
.
Context
.
Namespace
==
""
{
log
.
Debugf
(
"Overwriting --namespace flag with '%s'"
,
destination
.
Namespace
())
c
.
Overrides
.
Context
.
Namespace
=
destination
.
Namespace
()
}
return
nil
}
if
c
.
Overrides
.
Context
.
Namespace
==
""
{
log
.
Debugf
(
"Overwriting --namespace flag with '%s'"
,
destination
.
Namespace
())
c
.
Overrides
.
Context
.
Namespace
=
destination
.
Namespace
()
}
return
nil
return
fmt
.
Errorf
(
"Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address"
,
envName
,
destination
.
Server
())
}
return
errors
.
Errorf
(
"Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address"
,
envName
,
destination
.
Server
())
c
.
Overrides
.
Context
.
Namespace
=
destination
.
Namespace
()
c
.
Overrides
.
ClusterInfo
.
Server
=
server
// NOTE: ignore TLS verify since we don't have a CA cert to verify with.
c
.
Overrides
.
ClusterInfo
.
InsecureSkipTLSVerify
=
true
return
nil
}
cmd/actions.go
0 → 100644
View file @
7b6c9a54
// Copyright 2018 The ksonnet authors
//
//
// 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
cmd
import
"github.com/ksonnet/ksonnet/actions"
type
initName
int
const
(
actionInit
initName
=
iota
)
var
(
actionMap
=
map
[
initName
]
interface
{}{
actionInit
:
actions
.
RunInit
,
}
)
cmd/env.go
View file @
7b6c9a54
...
...
@@ -162,21 +162,25 @@ func commonEnvFlags(flags *pflag.FlagSet) (server, namespace, context string, er
func
resolveEnvFlags
(
flags
*
pflag
.
FlagSet
)
(
string
,
string
,
error
)
{
defaultNamespace
:=
"default"
server
,
n
s
,
context
,
err
:=
commonEnvFlags
(
flags
)
server
,
envN
s
,
context
,
err
:=
commonEnvFlags
(
flags
)
if
err
!=
nil
{
return
""
,
""
,
err
}
var
ctxNs
string
if
server
==
""
{
// server is not provided -- use the context.
server
,
n
s
,
err
=
envClientConfig
.
ResolveContext
(
context
)
server
,
ctxN
s
,
err
=
envClientConfig
.
ResolveContext
(
context
)
if
err
!=
nil
{
return
""
,
""
,
err
}
}
if
ns
==
""
{
ns
=
defaultNamespace
ns
:=
defaultNamespace
if
envNs
!=
""
{
ns
=
envNs
}
else
if
ctxNs
!=
""
{
ns
=
ctxNs
}
return
server
,
ns
,
nil
...
...
cmd/init.go
View file @
7b6c9a54
...
...
@@ -21,8 +21,8 @@ import (
"os"
"path/filepath"
"github.com/ksonnet/ksonnet/actions"
"github.com/ksonnet/ksonnet/client"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
...
...
@@ -84,7 +84,17 @@ var initCmd = &cobra.Command{
specFlag
=
initClientConfig
.
GetAPISpec
(
server
)
}
return
actions
.
RunInit
(
v
,
ok
:=
actionMap
[
actionInit
]
if
!
ok
{
return
errors
.
New
(
"init action does not exist"
)
}
fn
,
ok
:=
v
.
(
func
(
afero
.
Fs
,
string
,
string
,
string
,
string
,
string
)
error
)
if
!
ok
{
return
errors
.
New
(
"init action was not in the proper format"
)
}
return
fn
(
appFs
,
appName
,
appRoot
,
...
...
@@ -92,13 +102,6 @@ var initCmd = &cobra.Command{
server
,
namespace
,
)
// c, err := kubecfg.NewInitCmd(appName, appRoot, &specFlag, &server, &namespace)
// if err != nil {
// return err
// }
// return c.Run()
},
Long
:
`
The `
+
"`init`"
+
` command initializes a ksonnet application in a new directory,`
+
" `app-name`"
+
`.
...
...
cmd/init_test.go
View file @
7b6c9a54
...
...
@@ -15,7 +15,52 @@
package
cmd
import
"testing"
import
(
"os"
"path/filepath"
"testing"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func
withCmd
(
t
*
testing
.
T
,
cmd
*
cobra
.
Command
,
id
initName
,
override
interface
{},
fn
func
())
{
ogAction
:=
actionMap
[
id
]
actionMap
[
id
]
=
override
envConfig
:=
os
.
Getenv
(
"KUBECONFIG"
)
defer
func
()
{
actionMap
[
id
]
=
ogAction
os
.
Setenv
(
"KUBECONFIG"
,
envConfig
)
}()
fn
()
}
func
Test_initCmd
(
t
*
testing
.
T
)
{
override
:=
func
(
fs
afero
.
Fs
,
name
,
root
,
specFlag
,
server
,
namespace
string
)
error
{
wd
,
err
:=
os
.
Getwd
()
require
.
NoError
(
t
,
err
)
appRoot
:=
filepath
.
Join
(
wd
,
"app"
)
assert
.
Equal
(
t
,
"new-namespace"
,
namespace
)
assert
.
Equal
(
t
,
appRoot
,
root
)
assert
.
Equal
(
t
,
"app"
,
name
)
return
nil
}
withCmd
(
t
,
initCmd
,
actionInit
,
override
,
func
()
{
args
:=
[]
string
{
"init"
,
"app"
,
"--namespace"
,
"new-namespace"
,
"--server"
,
"http://127.0.0.1"
}
RootCmd
.
SetArgs
(
args
)
err
:=
RootCmd
.
Execute
()
require
.
NoError
(
t
,
err
)
})
}
func
Test_genKsRoot
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
...
...
pkg/appinit/init_test.go
View file @
7b6c9a54
...
...
@@ -31,7 +31,7 @@ func TestInit(t *testing.T) {
rootPath
:=
"/app"
specFlag
:=
"version:v1.8.7"
serverURI
:=
"http://example.com"
namespace
:=
"
default
"
namespace
:=
"
my-namespace
"
r
:=
&
mocks
.
Registry
{}
...
...
@@ -86,21 +86,21 @@ func TestInit(t *testing.T) {
}
require
.
NoError
(
t
,
err
)
checkApp
(
t
,
fs
,
rootPath
,
"v1.8.7"
)
checkApp
(
t
,
fs
,
rootPath
,
"v1.8.7"
,
namespace
)
})
}
}
func
checkApp
(
t
*
testing
.
T
,
fs
afero
.
Fs
,
rootPath
,
version
string
)
{
func
checkApp
(
t
*
testing
.
T
,
fs
afero
.
Fs
,
rootPath
,
version
,
namespace
string
)
{
expectedDirs
:=
[]
string
{
"app.yaml"
,
filepath
.
Join
(
".ksonnet"
,
"registries"
,
"testdata"
,
"registry.yaml"
),
filepath
.
Join
(
"components"
,
"params.libsonnet"
),
"vendor"
,
filepath
.
Join
(
"environments"
,
"base.libsonnet"
),
filepath
.
Join
(
"environments"
,
"default"
,
"main.jsonnet"
),
filepath
.
Join
(
"environments"
,
"default"
,
"params.libsonnet"
),
filepath
.
Join
(
"environments"
,
namespace
,
"main.jsonnet"
),
filepath
.
Join
(
"environments"
,
namespace
,
"params.libsonnet"
),
}
for
_
,
d
:=
range
expectedDirs
{
...
...
pkg/kubecfg/validate.go
View file @
7b6c9a54
...
...
@@ -33,7 +33,7 @@ type ValidateCmd struct {
}
func
(
c
ValidateCmd
)
Run
(
apiObjects
[]
*
unstructured
.
Unstructured
,
out
io
.
Writer
)
error
{
_
,
discovery
,
_
,
err
:=
client
.
Ini
tClient
(
c
.
Env
)
_
,
discovery
,
_
,
err
:=
c
.
C
lient
Config
.
Res
tClient
(
&
c
.
Env
)
if
err
!=
nil
{
return
err
}
...
...
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