| 
									
										
										
										
											2016-08-01 13:21:57 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | Copyright 2016 The Kubernetes Authors All rights reserved. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-08-06 00:14:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  | package driver | 
					
						
							| 
									
										
										
										
											2016-08-01 13:21:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  |     "reflect" | 
					
						
							|  |  |  |     "testing" | 
					
						
							| 
									
										
										
										
											2016-08-10 02:49:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  |     rspb "k8s.io/helm/pkg/proto/hapi/release" | 
					
						
							| 
									
										
										
										
											2016-08-01 13:21:57 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 02:28:41 +08:00
										 |  |  | func TestMemoryName(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  |     if mem := NewMemory(); mem.Name() != MemoryDriverName { | 
					
						
							|  |  |  |         t.Errorf("Expected name to be %q, got %q", MemoryDriverName, mem.Name()) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMemoryCreate(t *testing.T) { | 
					
						
							|  |  |  |     var tests = []struct { | 
					
						
							|  |  |  |         desc string | 
					
						
							|  |  |  |         rls  *rspb.Release | 
					
						
							|  |  |  |         err  bool | 
					
						
							|  |  |  |     }{ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "create should success", | 
					
						
							|  |  |  |             releaseStub("rls-c", 1, rspb.Status_DEPLOYED), | 
					
						
							|  |  |  |             false, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "create should fail (release already exists)", | 
					
						
							|  |  |  |             releaseStub("rls-a", 1, rspb.Status_DEPLOYED), | 
					
						
							|  |  |  |             true, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ts := tsFixtureMemory(t) | 
					
						
							|  |  |  |     for _, tt := range tests { | 
					
						
							|  |  |  |         key := testKey(tt.rls.Name, tt.rls.Version) | 
					
						
							|  |  |  |         rls := tt.rls | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if err := ts.Create(key, rls); err != nil { | 
					
						
							|  |  |  |             if !tt.err { | 
					
						
							|  |  |  |                 t.Fatalf("failed to create %q: %s", tt.desc, err) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-16 02:28:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-01 13:21:57 +08:00
										 |  |  | func TestMemoryGet(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  |     var tests = []struct { | 
					
						
							|  |  |  |         desc string | 
					
						
							|  |  |  |         key  string | 
					
						
							|  |  |  |         err  bool | 
					
						
							|  |  |  |     }{ | 
					
						
							|  |  |  |         {"release key should exist", "rls-a.v1", false}, | 
					
						
							|  |  |  |         {"release key should not exist", "rls-a.v5", true}, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ts := tsFixtureMemory(t) | 
					
						
							|  |  |  |     for _, tt := range tests { | 
					
						
							|  |  |  |         if _, err := ts.Get(tt.key); err != nil { | 
					
						
							|  |  |  |             if !tt.err { | 
					
						
							|  |  |  |                 t.Fatalf("Failed %q to get '%s': %q\n", tt.desc, tt.key, err) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-01 13:21:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  | func TestMemoryQuery(t *testing.T) { | 
					
						
							|  |  |  |     var tests = []struct { | 
					
						
							|  |  |  |         desc string | 
					
						
							|  |  |  |         xlen int | 
					
						
							|  |  |  |         lbs  map[string]string | 
					
						
							|  |  |  |     }{ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "should be 2 query results", | 
					
						
							|  |  |  |             2, | 
					
						
							|  |  |  |             map[string]string{"STATUS": "DEPLOYED"}, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ts := tsFixtureMemory(t) | 
					
						
							|  |  |  |     for _, tt := range tests { | 
					
						
							|  |  |  |         l, err := ts.Query(tt.lbs) | 
					
						
							|  |  |  |         if err != nil { | 
					
						
							|  |  |  |             t.Fatalf("Failed to query: %s\n", err) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if tt.xlen != len(l) { | 
					
						
							|  |  |  |             t.Fatalf("Expected %d results, actual %d\n", tt.xlen, len(l)) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-01 13:21:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMemoryUpdate(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  |     var tests = []struct { | 
					
						
							|  |  |  |         desc string | 
					
						
							|  |  |  |         key  string | 
					
						
							|  |  |  |         rls  *rspb.Release | 
					
						
							|  |  |  |         err  bool | 
					
						
							|  |  |  |     }{ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "update release status", | 
					
						
							|  |  |  |             "rls-a.v4", | 
					
						
							|  |  |  |             releaseStub("rls-a", 4, rspb.Status_SUPERSEDED), | 
					
						
							|  |  |  |             false, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "update release does not exist", | 
					
						
							|  |  |  |             "rls-z.v1", | 
					
						
							|  |  |  |             releaseStub("rls-z", 1, rspb.Status_DELETED), | 
					
						
							|  |  |  |             true, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ts := tsFixtureMemory(t) | 
					
						
							|  |  |  |     for _, tt := range tests { | 
					
						
							|  |  |  |         if err := ts.Update(tt.key, tt.rls); err != nil { | 
					
						
							|  |  |  |             if !tt.err { | 
					
						
							|  |  |  |                 t.Fatalf("Failed %q: %s\n", tt.desc, err) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         r, err := ts.Get(tt.key) | 
					
						
							|  |  |  |         if err != nil { | 
					
						
							|  |  |  |             t.Fatalf("Failed to get: %s\n", err) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if !reflect.DeepEqual(r, tt.rls) { | 
					
						
							|  |  |  |             t.Fatalf("Expected %s, actual %s\n", tt.rls, r) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-01 13:21:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMemoryDelete(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-09-02 03:07:12 +08:00
										 |  |  |     var tests = []struct { | 
					
						
							|  |  |  |         desc string | 
					
						
							|  |  |  |         key  string | 
					
						
							|  |  |  |         err  bool | 
					
						
							|  |  |  |     }{ | 
					
						
							|  |  |  |         {"release key should exist", "rls-a.v1", false}, | 
					
						
							|  |  |  |         {"release key should not exist", "rls-a.v5", true}, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ts := tsFixtureMemory(t) | 
					
						
							|  |  |  |     for _, tt := range tests { | 
					
						
							|  |  |  |         if _, err := ts.Delete(tt.key); err != nil { | 
					
						
							|  |  |  |             if !tt.err { | 
					
						
							|  |  |  |                 t.Fatalf("Failed %q to get '%s': %q\n", tt.desc, tt.key, err) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-01 13:34:50 +08:00
										 |  |  | } |