agola/internal/services/config/config_test.go
Simone Gotti d2b09d854f *: use new errors handling library
Implement a new error handling library based on pkg/errors. It provides
stack saving on wrapping and exports some function to add stack saving
also to external errors.
It also implements custom zerolog error formatting without adding too
much verbosity by just printing the chain error file:line without a full
stack trace of every error.

* Add a --detailed-errors options to print error with they full chain
* Wrap all error returns. Use errors.WithStack to wrap without adding a
  new messsage and error.Wrap[f] to add a message.
* Add golangci-lint wrapcheck to check that external packages errors are
  wrapped. This won't check that internal packages error are wrapped.
  But we want also to ensure this case so we'll have to find something
  else to check also these.
2022-02-28 12:49:13 +01:00

258 lines
5.8 KiB
Go

// Copyright 2019 Sorint.lab
//
// 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 config
import (
"io/ioutil"
"os"
"path"
"testing"
"agola.io/agola/internal/errors"
)
func TestParseConfig(t *testing.T) {
tests := []struct {
name string
services []string
in string
err error
}{
{
name: "test config for all-base components and executor",
services: []string{"all-base", "executor"},
in: `
gateway:
apiExposedURL: "http://localhost:8000"
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
gitserverURL: "http://localhost:4003"
web:
listenAddress: ":8000"
tokenSigning:
method: hmac
key: supersecretsigningkey
adminToken: "admintoken"
scheduler:
runserviceURL: "http://localhost:4000"
notification:
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
etcd:
endpoints: "http://localhost:2379"
configstore:
dataDir: /data/agola/configstore
etcd:
endpoints: "http://localhost:2379"
objectStorage:
type: posix
path: /agola/configstore/ost
web:
listenAddress: ":4002"
runservice:
#debug: true
dataDir: /opt/data/agola/runservice
etcd:
endpoints: "http://localhost:2379"
objectStorage:
type: posix
path: /agola/runservice/ost
web:
listenAddress: ":4000"
executor:
allowPrivilegedContainers: true
dataDir: /data/agola/executor
toolboxPath: ./bin
runserviceURL: "http://localhost:4000"
web:
listenAddress: ":4001"
activeTasksLimit: 5
driver:
type: docker
gitserver:
dataDir: /data/agola/gitserver
gatewayURL: "http://localhost:8000"
web:
listenAddress: ":4003"`,
},
{
name: "test config for all-base components",
services: []string{"all-base"},
in: `
gateway:
apiExposedURL: "http://localhost:8000"
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
gitserverURL: "http://localhost:4003"
web:
listenAddress: ":8000"
tokenSigning:
method: hmac
key: supersecretsigningkey
adminToken: "admintoken"
scheduler:
runserviceURL: "http://localhost:4000"
notification:
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
etcd:
endpoints: "http://localhost:2379"
configstore:
dataDir: /data/agola/configstore
etcd:
endpoints: "http://localhost:2379"
objectStorage:
type: posix
path: /agola/configstore/ost
web:
listenAddress: ":4002"
runservice:
dataDir: /data/agola/runservice
etcd:
endpoints: "http://localhost:2379"
objectStorage:
type: posix
path: /agola/runservice/ost
web:
listenAddress: ":4000"
gitserver:
dataDir: /data/agola/gitserver
gatewayURL: "http://localhost:8000"
web:
listenAddress: ":4003"`,
},
{
name: "test config for gateway, scheduler and notification",
services: []string{"gateway", "scheduler", "notification"},
in: `
gateway:
apiExposedURL: "http://localhost:8000"
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
gitserverURL: "http://localhost:4003"
web:
listenAddress: ":8000"
tokenSigning:
method: hmac
key: supersecretsigningkey
adminToken: "admintoken"
scheduler:
runserviceURL: "http://localhost:4000"
notification:
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
etcd:
endpoints: "http://localhost:2379"
configstore:
dataDir:
runservice:
dataDir:
gitserver:
dataDir:`,
},
{
name: "test config for gateway, scheduler, notification and gitserver without dataDir",
services: []string{"gateway", "scheduler", "notification", "gitserver"},
in: `
gateway:
apiExposedURL: "http://localhost:8000"
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
gitserverURL: "http://localhost:4003"
web:
listenAddress: ":8000"
tokenSigning:
method: hmac
key: supersecretsigningkey
adminToken: "admintoken"
scheduler:
runserviceURL: "http://localhost:4000"
notification:
webExposedURL: "http://localhost:8000"
runserviceURL: "http://localhost:4000"
configstoreURL: "http://localhost:4002"
etcd:
endpoints: "http://localhost:2379"
configstore:
dataDir:
runservice:
dataDir:
gitserver:
dataDir:`,
err: errors.Errorf("git server dataDir is empty"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "ParseConfig")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
content := []byte(tt.in)
err = ioutil.WriteFile(path.Join(dir, "config.yml"), content, 0644)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if _, err := Parse(path.Join(dir, "config.yml"), tt.services); err != nil {
if tt.err == nil {
t.Fatalf("got error: %v, expected no error", err)
}
if err.Error() != tt.err.Error() {
t.Fatalf("got error: %v, want error: %v", err, tt.err)
}
} else {
if tt.err != nil {
t.Fatalf("got nil error, want error: %v", tt.err)
}
}
})
}
}