|
| 1 | +package net |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "github.com/zalando/skipper/net/redistest" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRemoteCache(t *testing.T) { |
| 12 | + t.Logf("create redis..") |
| 13 | + redisAddr, done := redistest.NewTestRedis(t) |
| 14 | + defer done() |
| 15 | + if redisAddr == "" { |
| 16 | + t.Fatal("Failed to create redis 1") |
| 17 | + } |
| 18 | + |
| 19 | + redisAddr2, done2 := redistest.NewTestRedis(t) |
| 20 | + defer done2() |
| 21 | + if redisAddr2 == "" { |
| 22 | + t.Fatal("Failed to create redis 2") |
| 23 | + } |
| 24 | + |
| 25 | + rc := RemoteCache{ |
| 26 | + Client: NewRedisRingClient(&RedisOptions{ |
| 27 | + Addrs: []string{redisAddr, redisAddr2}, |
| 28 | + }), |
| 29 | + } |
| 30 | + defer rc.Close() |
| 31 | + |
| 32 | + if err := rc.Put(context.Background(), "foo", []byte("bar")); err != nil { |
| 33 | + t.Fatalf("Failed to put: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + if v, err := rc.Get(context.Background(), "foo"); err != nil { |
| 37 | + t.Fatalf("Failed to get: %v", err) |
| 38 | + } else { |
| 39 | + t.Logf("%T %v %s", v, v, v) |
| 40 | + if string(v) != "bar" { |
| 41 | + t.Fatalf("Failed to get result, got: %q", string(v)) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if err := rc.Delete(context.Background(), "foo"); err != nil { |
| 46 | + t.Fatalf("Failed to delete: %v", err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestInmemoryCache(t *testing.T) { |
| 51 | + rc := &InmemoryCache{} |
| 52 | + |
| 53 | + if _, err := rc.Get(context.Background(), "foo"); err == nil { |
| 54 | + t.Fatal(`Failed can not get "foo" on empty cache`) |
| 55 | + } |
| 56 | + |
| 57 | + if err := rc.Put(context.Background(), "foo", []byte("bar")); err != nil { |
| 58 | + t.Fatalf("Failed to put: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + if v, err := rc.Get(context.Background(), "foo"); err != nil { |
| 62 | + t.Fatalf("Failed to get: %v", err) |
| 63 | + } else { |
| 64 | + t.Logf("%T %v %s", v, v, v) |
| 65 | + } |
| 66 | + |
| 67 | + if err := rc.Delete(context.Background(), "foo"); err != nil { |
| 68 | + t.Fatalf("Failed to delete: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + if err := rc.Put(context.Background(), "foo2", []byte("ü")); err != nil { |
| 72 | + t.Fatalf("Failed to put: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + if v, err := rc.Get(context.Background(), "foo2"); err != nil { |
| 76 | + t.Fatalf("Failed to get: %v", err) |
| 77 | + } else { |
| 78 | + t.Logf("%T %v %s", v, v, v) |
| 79 | + } |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +func TestLetsencrypt(t *testing.T) { |
| 84 | + invalidDomain := "s_.example.org" |
| 85 | + if validateDomain(invalidDomain) { |
| 86 | + t.Fatalf("Failed to validate invalid domain %q", invalidDomain) |
| 87 | + } |
| 88 | + validDomain := "example.org" |
| 89 | + if !validateDomain(validDomain) { |
| 90 | + t.Fatalf("Failed to validate valid domain %q", validDomain) |
| 91 | + } |
| 92 | + |
| 93 | + le := NewLetsencrypt(&InmemoryCache{}, "skipper@example.org", "https://acme-staging-v02.api.letsencrypt.org/directory", "skipper-test TestLetsencrypt", []string{validDomain}) |
| 94 | + defer le.Close() |
| 95 | + if le.manager.Client != nil { |
| 96 | + dir, err := le.manager.Client.Discover(context.TODO()) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("Failed to discover: %v", err) |
| 99 | + } |
| 100 | + t.Logf("order: %s", dir.OrderURL) |
| 101 | + |
| 102 | + defer func() { |
| 103 | + if le.manager.Client.HTTPClient != nil { |
| 104 | + le.manager.Client.HTTPClient.CloseIdleConnections() |
| 105 | + } |
| 106 | + }() |
| 107 | + } |
| 108 | + |
| 109 | + require.NotNil(t, le.Client(), "client should not be nil") |
| 110 | + require.NotNil(t, le.TLSConfig(), "TLSConfig should not be nil") |
| 111 | + require.NotNil(t, le.Handler(nil), "http.Handler should not be nil") |
| 112 | + |
| 113 | + li := le.Listener() |
| 114 | + defer li.Close() |
| 115 | + t.Logf("listener %v", li.Addr()) |
| 116 | +} |
0 commit comments