1// Copyright 2017 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package embed
16
17import (
18	"fmt"
19	"io/ioutil"
20	"net/url"
21	"os"
22	"testing"
23
24	"go.etcd.io/etcd/server/v3/auth"
25)
26
27// TestStartEtcdWrongToken ensures that StartEtcd with wrong configs returns with error.
28func TestStartEtcdWrongToken(t *testing.T) {
29	tdir, err := ioutil.TempDir(t.TempDir(), "token-test")
30
31	if err != nil {
32		t.Fatal(err)
33	}
34
35	cfg := NewConfig()
36
37	// Similar to function in integration/embed/embed_test.go for setting up Config.
38	urls := newEmbedURLs(2)
39	curls := []url.URL{urls[0]}
40	purls := []url.URL{urls[1]}
41	cfg.LCUrls, cfg.ACUrls = curls, curls
42	cfg.LPUrls, cfg.APUrls = purls, purls
43	cfg.InitialCluster = ""
44	for i := range purls {
45		cfg.InitialCluster += ",default=" + purls[i].String()
46	}
47	cfg.InitialCluster = cfg.InitialCluster[1:]
48	cfg.Dir = tdir
49	cfg.AuthToken = "wrong-token"
50
51	if _, err = StartEtcd(cfg); err != auth.ErrInvalidAuthOpts {
52		t.Fatalf("expected %v, got %v", auth.ErrInvalidAuthOpts, err)
53	}
54}
55
56func newEmbedURLs(n int) (urls []url.URL) {
57	scheme := "unix"
58	for i := 0; i < n; i++ {
59		u, _ := url.Parse(fmt.Sprintf("%s://localhost:%d%06d", scheme, os.Getpid(), i))
60		urls = append(urls, *u)
61	}
62	return urls
63}
64