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
703b5af9
Unverified
Commit
703b5af9
authored
Aug 23, 2021
by
Derek Nola
Committed by
GitHub
Aug 23, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable K3s integration test to run on existing cluster (#3892)
* Made it possible to run int tests on existing cluster Signed-off-by:
dereknola
<
derek.nola@suse.com
>
parent
58315fe1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
23 deletions
+92
-23
etcd_int_test.go
pkg/etcd/etcd_int_test.go
+20
-18
TESTING.md
tests/TESTING.md
+6
-0
localstorage_int_test.go
tests/integration/localstorage_int_test.go
+15
-5
cmd.go
tests/util/cmd.go
+51
-0
No files found.
pkg/etcd/etcd_int_test.go
View file @
703b5af9
...
@@ -12,13 +12,21 @@ import (
...
@@ -12,13 +12,21 @@ import (
)
)
var
server
*
testutil
.
K3sServer
var
server
*
testutil
.
K3sServer
var
serverArgs
=
[]
string
{
"--cluster-init"
}
var
_
=
BeforeSuite
(
func
()
{
var
_
=
BeforeSuite
(
func
()
{
var
err
error
if
!
testutil
.
IsExistingServer
()
{
server
,
err
=
testutil
.
K3sStartServer
(
"--cluster-init"
)
var
err
error
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
server
,
err
=
testutil
.
K3sStartServer
(
serverArgs
...
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
}
})
})
var
_
=
Describe
(
"etcd snapshots"
,
func
()
{
var
_
=
Describe
(
"etcd snapshots"
,
func
()
{
BeforeEach
(
func
()
{
if
!
testutil
.
ServerArgsPresent
(
serverArgs
)
{
Skip
(
"Test needs k3s server with: "
+
strings
.
Join
(
serverArgs
,
" "
))
}
})
When
(
"a new etcd is created"
,
func
()
{
When
(
"a new etcd is created"
,
func
()
{
It
(
"starts up with no problems"
,
func
()
{
It
(
"starts up with no problems"
,
func
()
{
Eventually
(
func
()
(
string
,
error
)
{
Eventually
(
func
()
(
string
,
error
)
{
...
@@ -44,9 +52,6 @@ var _ = Describe("etcd snapshots", func() {
...
@@ -44,9 +52,6 @@ var _ = Describe("etcd snapshots", func() {
})
})
})
})
When
(
"saving a custom name"
,
func
()
{
When
(
"saving a custom name"
,
func
()
{
It
(
"starts with no snapshots"
,
func
()
{
Expect
(
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"ls"
))
.
To
(
BeEmpty
())
})
It
(
"saves an etcd snapshot with a custom name"
,
func
()
{
It
(
"saves an etcd snapshot with a custom name"
,
func
()
{
Expect
(
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"save"
,
"--name"
,
"ALIVEBEEF"
))
.
Expect
(
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"save"
,
"--name"
,
"ALIVEBEEF"
))
.
To
(
ContainSubstring
(
"Saving etcd snapshot to /var/lib/rancher/k3s/server/db/snapshots/ALIVEBEEF"
))
To
(
ContainSubstring
(
"Saving etcd snapshot to /var/lib/rancher/k3s/server/db/snapshots/ALIVEBEEF"
))
...
@@ -62,9 +67,6 @@ var _ = Describe("etcd snapshots", func() {
...
@@ -62,9 +67,6 @@ var _ = Describe("etcd snapshots", func() {
})
})
})
})
When
(
"using etcd snapshot prune"
,
func
()
{
When
(
"using etcd snapshot prune"
,
func
()
{
It
(
"starts with no snapshots"
,
func
()
{
Expect
(
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"ls"
))
.
To
(
BeEmpty
())
})
It
(
"saves 3 different snapshots"
,
func
()
{
It
(
"saves 3 different snapshots"
,
func
()
{
Expect
(
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"save"
,
"-name"
,
"PRUNE_TEST"
))
.
Expect
(
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"save"
,
"-name"
,
"PRUNE_TEST"
))
.
To
(
ContainSubstring
(
"Saving current etcd snapshot set to k3s-etcd-snapshots"
))
To
(
ContainSubstring
(
"Saving current etcd snapshot set to k3s-etcd-snapshots"
))
...
@@ -79,10 +81,9 @@ var _ = Describe("etcd snapshots", func() {
...
@@ -79,10 +81,9 @@ var _ = Describe("etcd snapshots", func() {
It
(
"lists all 3 snapshots"
,
func
()
{
It
(
"lists all 3 snapshots"
,
func
()
{
lsResult
,
err
:=
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"ls"
)
lsResult
,
err
:=
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"ls"
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
sepLines
:=
strings
.
FieldsFunc
(
lsResult
,
func
(
c
rune
)
bool
{
reg
,
err
:=
regexp
.
Compile
(
`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`
)
return
c
==
'\n'
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
})
sepLines
:=
reg
.
FindAllString
(
lsResult
,
-
1
)
Expect
(
lsResult
)
.
To
(
MatchRegexp
(
`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`
))
Expect
(
sepLines
)
.
To
(
HaveLen
(
3
))
Expect
(
sepLines
)
.
To
(
HaveLen
(
3
))
})
})
It
(
"prunes snapshots down to 2"
,
func
()
{
It
(
"prunes snapshots down to 2"
,
func
()
{
...
@@ -90,10 +91,9 @@ var _ = Describe("etcd snapshots", func() {
...
@@ -90,10 +91,9 @@ var _ = Describe("etcd snapshots", func() {
To
(
BeEmpty
())
To
(
BeEmpty
())
lsResult
,
err
:=
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"ls"
)
lsResult
,
err
:=
testutil
.
K3sCmd
(
"etcd-snapshot"
,
"ls"
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
sepLines
:=
strings
.
FieldsFunc
(
lsResult
,
func
(
c
rune
)
bool
{
reg
,
err
:=
regexp
.
Compile
(
`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`
)
return
c
==
'\n'
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
})
sepLines
:=
reg
.
FindAllString
(
lsResult
,
-
1
)
Expect
(
lsResult
)
.
To
(
MatchRegexp
(
`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`
))
Expect
(
sepLines
)
.
To
(
HaveLen
(
2
))
Expect
(
sepLines
)
.
To
(
HaveLen
(
2
))
})
})
It
(
"cleans up remaining snapshots"
,
func
()
{
It
(
"cleans up remaining snapshots"
,
func
()
{
...
@@ -110,7 +110,9 @@ var _ = Describe("etcd snapshots", func() {
...
@@ -110,7 +110,9 @@ var _ = Describe("etcd snapshots", func() {
})
})
var
_
=
AfterSuite
(
func
()
{
var
_
=
AfterSuite
(
func
()
{
Expect
(
testutil
.
K3sKillServer
(
server
))
.
To
(
Succeed
())
if
!
testutil
.
IsExistingServer
()
{
Expect
(
testutil
.
K3sKillServer
(
server
))
.
To
(
Succeed
())
}
})
})
func
Test_IntegrationEtcd
(
t
*
testing
.
T
)
{
func
Test_IntegrationEtcd
(
t
*
testing
.
T
)
{
...
...
tests/TESTING.md
View file @
703b5af9
...
@@ -71,10 +71,16 @@ See the [local storage test](https://github.com/k3s-io/k3s/blob/master/tests/int
...
@@ -71,10 +71,16 @@ See the [local storage test](https://github.com/k3s-io/k3s/blob/master/tests/int
### Running
### Running
Integration tests can be run with no k3s cluster present, each test will spin up and kill the appropriate k3s server it needs.
```
bash
```
bash
go
test
./pkg/... ./tests/...
-run
Integration
go
test
./pkg/... ./tests/...
-run
Integration
```
```
Integration tests can also be run on an existing cluster via compile time flag, tests will skip if the server is not configured correctly.
```
go test -ldflags "-X 'github.com/rancher/k3s/tests/util.existingServer=True'" ./pkg/... ./tests/... -run Integration
```
__
_
__
_
## End-to-End (E2E) Tests
## End-to-End (E2E) Tests
...
...
tests/integration/localstorage_int_test.go
View file @
703b5af9
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"fmt"
"fmt"
"os"
"os"
"regexp"
"regexp"
"strings"
"testing"
"testing"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/ginkgo"
...
@@ -12,13 +13,21 @@ import (
...
@@ -12,13 +13,21 @@ import (
)
)
var
server
*
testutil
.
K3sServer
var
server
*
testutil
.
K3sServer
var
serverArgs
=
[]
string
{
"--cluster-init"
}
var
_
=
BeforeSuite
(
func
()
{
var
_
=
BeforeSuite
(
func
()
{
var
err
error
if
!
testutil
.
IsExistingServer
()
{
server
,
err
=
testutil
.
K3sStartServer
(
"--cluster-init"
)
var
err
error
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
server
,
err
=
testutil
.
K3sStartServer
(
serverArgs
...
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
}
})
})
var
_
=
Describe
(
"local storage"
,
func
()
{
var
_
=
Describe
(
"local storage"
,
func
()
{
BeforeEach
(
func
()
{
if
!
testutil
.
ServerArgsPresent
(
serverArgs
)
{
Skip
(
"Test needs k3s server with: "
+
strings
.
Join
(
serverArgs
,
" "
))
}
})
When
(
"a new local storage is created"
,
func
()
{
When
(
"a new local storage is created"
,
func
()
{
It
(
"starts up with no problems"
,
func
()
{
It
(
"starts up with no problems"
,
func
()
{
Eventually
(
func
()
(
string
,
error
)
{
Eventually
(
func
()
(
string
,
error
)
{
...
@@ -66,11 +75,12 @@ var _ = Describe("local storage", func() {
...
@@ -66,11 +75,12 @@ var _ = Describe("local storage", func() {
})
})
var
_
=
AfterSuite
(
func
()
{
var
_
=
AfterSuite
(
func
()
{
Expect
(
testutil
.
K3sKillServer
(
server
))
.
To
(
Succeed
())
if
!
testutil
.
IsExistingServer
()
{
Expect
(
testutil
.
K3sKillServer
(
server
))
.
To
(
Succeed
())
}
})
})
func
Test_IntegrationLocalStorage
(
t
*
testing
.
T
)
{
func
Test_IntegrationLocalStorage
(
t
*
testing
.
T
)
{
RegisterFailHandler
(
Fail
)
RegisterFailHandler
(
Fail
)
RunSpecs
(
t
,
"Local Storage Suite"
)
RunSpecs
(
t
,
"Local Storage Suite"
)
}
}
tests/util/cmd.go
View file @
703b5af9
...
@@ -2,6 +2,7 @@ package util
...
@@ -2,6 +2,7 @@ package util
import
(
import
(
"bufio"
"bufio"
"encoding/json"
"os"
"os"
"os/exec"
"os/exec"
"os/user"
"os/user"
...
@@ -11,7 +12,18 @@ import (
...
@@ -11,7 +12,18 @@ import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
)
// Compile-time variable
var
existingServer
string
=
"False"
func
findK3sExecutable
()
string
{
func
findK3sExecutable
()
string
{
// if running on an existing cluster, it maybe installed via k3s.service
// or run manually from dist/artifacts/k3s
if
IsExistingServer
()
{
k3sBin
,
err
:=
exec
.
LookPath
(
"k3s"
)
if
err
==
nil
{
return
k3sBin
}
}
k3sBin
:=
"dist/artifacts/k3s"
k3sBin
:=
"dist/artifacts/k3s"
for
{
for
{
_
,
err
:=
os
.
Stat
(
k3sBin
)
_
,
err
:=
os
.
Stat
(
k3sBin
)
...
@@ -33,6 +45,10 @@ func IsRoot() bool {
...
@@ -33,6 +45,10 @@ func IsRoot() bool {
return
currentUser
.
Uid
==
"0"
return
currentUser
.
Uid
==
"0"
}
}
func
IsExistingServer
()
bool
{
return
existingServer
==
"True"
}
// K3sCmd launches the provided K3s command via exec. Command blocks until finished.
// K3sCmd launches the provided K3s command via exec. Command blocks until finished.
// Command output from both Stderr and Stdout is provided via string.
// Command output from both Stderr and Stdout is provided via string.
// cmdEx1, err := K3sCmd("etcd-snapshot", "ls")
// cmdEx1, err := K3sCmd("etcd-snapshot", "ls")
...
@@ -52,6 +68,41 @@ func K3sCmd(cmdName string, cmdArgs ...string) (string, error) {
...
@@ -52,6 +68,41 @@ func K3sCmd(cmdName string, cmdArgs ...string) (string, error) {
return
string
(
byteOut
),
err
return
string
(
byteOut
),
err
}
}
func
contains
(
source
[]
string
,
target
string
)
bool
{
for
_
,
s
:=
range
source
{
if
s
==
target
{
return
true
}
}
return
false
}
// ServerArgsPresent checks if the given arguments are found in the running k3s server
func
ServerArgsPresent
(
neededArgs
[]
string
)
bool
{
currentArgs
:=
K3sServerArgs
()
for
_
,
arg
:=
range
neededArgs
{
if
!
contains
(
currentArgs
,
arg
)
{
return
false
}
}
return
true
}
// K3sServerArgs returns the list of arguments that the k3s server launched with
func
K3sServerArgs
()
[]
string
{
results
,
err
:=
K3sCmd
(
"kubectl"
,
"get"
,
"nodes"
,
"-o"
,
`jsonpath='{.items[0].metadata.annotations.k3s\.io/node-args}'`
)
if
err
!=
nil
{
return
nil
}
res
:=
strings
.
Replace
(
results
,
"'"
,
""
,
-
1
)
var
args
[]
string
if
err
:=
json
.
Unmarshal
([]
byte
(
res
),
&
args
);
err
!=
nil
{
logrus
.
Error
(
err
)
return
nil
}
return
args
}
func
FindStringInCmdAsync
(
scanner
*
bufio
.
Scanner
,
target
string
)
bool
{
func
FindStringInCmdAsync
(
scanner
*
bufio
.
Scanner
,
target
string
)
bool
{
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
if
strings
.
Contains
(
scanner
.
Text
(),
target
)
{
if
strings
.
Contains
(
scanner
.
Text
(),
target
)
{
...
...
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