1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-24 08:02:18 +02:00

Add some tests (#3030)

This commit is contained in:
qwerty287 2023-12-27 10:36:49 +01:00 committed by GitHub
parent 5cb0ae053a
commit e575ffe72d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 117 additions and 2 deletions

View File

@ -0,0 +1,14 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/cmd/server/docs"
)
func TestSetupSwaggerStaticConfig(t *testing.T) {
setupSwaggerStaticConfig()
assert.Equal(t, "/api", docs.SwaggerInfo.BasePath)
}

View File

@ -0,0 +1,27 @@
package common
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
windowsScriptBase64 = "CiRFcnJvckFjdGlvblByZWZlcmVuY2UgPSAnU3RvcCc7CiZjbWQgL2MgIm1rZGlyIGM6XHJvb3QiOwppZiAoJEVudjpDSV9ORVRSQ19NQUNISU5FKSB7CiRuZXRyYz1bc3RyaW5nXTo6Rm9ybWF0KCJ7MH1cX25ldHJjIiwkRW52OkhPTUUpOwoibWFjaGluZSAkRW52OkNJX05FVFJDX01BQ0hJTkUiID4+ICRuZXRyYzsKImxvZ2luICRFbnY6Q0lfTkVUUkNfVVNFUk5BTUUiID4+ICRuZXRyYzsKInBhc3N3b3JkICRFbnY6Q0lfTkVUUkNfUEFTU1dPUkQiID4+ICRuZXRyYzsKfTsKW0Vudmlyb25tZW50XTo6U2V0RW52aXJvbm1lbnRWYXJpYWJsZSgiQ0lfTkVUUkNfUEFTU1dPUkQiLCRudWxsKTsKW0Vudmlyb25tZW50XTo6U2V0RW52aXJvbm1lbnRWYXJpYWJsZSgiQ0lfU0NSSVBUIiwkbnVsbCk7CgpXcml0ZS1PdXRwdXQgKCcrICJlY2hvIGhlbGxvIHdvcmxkIicpOwomIGVjaG8gaGVsbG8gd29ybGQ7IGlmICgkTEFTVEVYSVRDT0RFIC1uZSAwKSB7ZXhpdCAkTEFTVEVYSVRDT0RFfQoK"
posixScriptBase64 = "CmlmIFsgLW4gIiRDSV9ORVRSQ19NQUNISU5FIiBdOyB0aGVuCmNhdCA8PEVPRiA+ICRIT01FLy5uZXRyYwptYWNoaW5lICRDSV9ORVRSQ19NQUNISU5FCmxvZ2luICRDSV9ORVRSQ19VU0VSTkFNRQpwYXNzd29yZCAkQ0lfTkVUUkNfUEFTU1dPUkQKRU9GCmNobW9kIDA2MDAgJEhPTUUvLm5ldHJjCmZpCnVuc2V0IENJX05FVFJDX1VTRVJOQU1FCnVuc2V0IENJX05FVFJDX1BBU1NXT1JECnVuc2V0IENJX1NDUklQVAoKZWNobyArICdlY2hvIGhlbGxvIHdvcmxkJwplY2hvIGhlbGxvIHdvcmxkCg=="
)
func TestGenerateContainerConf(t *testing.T) {
gotEnv, gotEntry, gotCmd := GenerateContainerConf([]string{"echo hello world"}, "windows")
assert.Equal(t, windowsScriptBase64, gotEnv["CI_SCRIPT"])
assert.Equal(t, "c:\\root", gotEnv["HOME"])
assert.Equal(t, "powershell.exe", gotEnv["SHELL"])
assert.Equal(t, []string{"powershell", "-noprofile", "-noninteractive", "-command"}, gotEntry)
assert.Equal(t, []string{"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Env:CI_SCRIPT)) | iex"}, gotCmd)
gotEnv, gotEntry, gotCmd = GenerateContainerConf([]string{"echo hello world"}, "linux")
assert.Equal(t, posixScriptBase64, gotEnv["CI_SCRIPT"])
assert.Equal(t, "/root", gotEnv["HOME"])
assert.Equal(t, "/bin/sh", gotEnv["SHELL"])
assert.Equal(t, []string{"/bin/sh", "-c"}, gotEntry)
assert.Equal(t, []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"}, gotCmd)
}

View File

@ -26,7 +26,7 @@ type builtin struct {
globals []*model.Environ
}
// Parse returns a EnvironService based on a string slice where key and value are separated by a ":" delimiter.
// Parse returns a model.EnvironService based on a string slice where key and value are separated by a ":" delimiter.
func Parse(params []string) model.EnvironService {
var globals []*model.Environ

View File

@ -0,0 +1,26 @@
package environments
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
service := Parse([]string{})
env, err := service.EnvironList(nil)
assert.NoError(t, err)
assert.Empty(t, env)
service = Parse([]string{"ENV:value"})
env, err = service.EnvironList(nil)
assert.NoError(t, err)
assert.Len(t, env, 1)
assert.Equal(t, env[0].Name, "ENV")
assert.Equal(t, env[0].Value, "value")
service = Parse([]string{"ENV:value", "ENV2:value2"})
env, err = service.EnvironList(nil)
assert.NoError(t, err)
assert.Len(t, env, 2)
}

View File

@ -0,0 +1,18 @@
package permissions
import (
"testing"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
)
func TestAdmins(t *testing.T) {
a := NewAdmins([]string{"woodpecker-ci"})
assert.True(t, a.IsAdmin(&model.User{Login: "woodpecker-ci"}))
assert.False(t, a.IsAdmin(&model.User{Login: "not-woodpecker-ci"}))
empty := NewAdmins([]string{})
assert.False(t, empty.IsAdmin(&model.User{Login: "woodpecker-ci"}))
assert.False(t, empty.IsAdmin(&model.User{Login: "not-woodpecker-ci"}))
}

View File

@ -0,0 +1,20 @@
package permissions
import (
"testing"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
)
func TestOrgs(t *testing.T) {
o := NewOrgs([]string{"woodpecker-ci"})
assert.True(t, o.IsConfigured)
assert.True(t, o.IsMember([]*model.Team{{Login: "woodpecker-ci"}}))
assert.False(t, o.IsMember([]*model.Team{{Login: "not-woodpecker-ci"}}))
empty := NewOrgs([]string{})
assert.False(t, empty.IsConfigured)
assert.False(t, empty.IsMember([]*model.Team{{Login: "woodpecker-ci"}}))
assert.False(t, empty.IsMember([]*model.Team{{Login: "not-woodpecker-ci"}}))
}

View File

@ -58,7 +58,7 @@ func sliceToCountMap[E comparable](list []E) map[E]int {
return m
}
// sliceToMap is a helper function to convert a string slice to a map.
// SliceToBoolMap is a helper function to convert a string slice to a map.
func SliceToBoolMap(s []string) map[string]bool {
v := map[string]bool{}
for _, ss := range s {

View File

@ -58,3 +58,13 @@ func TestEqualSliceValues(t *testing.T) {
assert.True(t, EqualSliceValues([]bool{true, false, false}, []bool{false, false, true}))
assert.False(t, EqualSliceValues([]bool{true, false, false}, []bool{true, false, true}))
}
func TestSliceToBoolMap(t *testing.T) {
assert.Equal(t, map[string]bool{
"a": true,
"b": true,
"c": true,
}, SliceToBoolMap([]string{"a", "b", "c"}))
assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{}))
assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{""}))
}