func setup(t *testing.T) ( rcStore rcstore.Store, kpStore fakeKpStore, applicator labels.Applicator, rc ReplicationController) { rcStore = rcstore.NewFake() manifestBuilder := pods.NewManifestBuilder() manifestBuilder.SetID("testPod") manifest := manifestBuilder.GetManifest() nodeSelector := klabels.Everything().Add("nodeQuality", klabels.EqualsOperator, []string{"good"}) podLabels := map[string]string{"podTest": "successful"} rcData, err := rcStore.Create(manifest, nodeSelector, podLabels) Assert(t).IsNil(err, "expected no error creating request") kpStore = fakeKpStore{manifests: make(map[string]pods.Manifest)} applicator = labels.NewFakeApplicator() rc = New( rcData, &kpStore, rcStore, NewApplicatorScheduler(applicator), applicator, logging.DefaultLogger, ) return }
func TestBasicMatch(t *testing.T) { c := &consulApplicator{ kv: &fakeLabelStore{data: map[string][]byte{}}, logger: logging.DefaultLogger, } Assert(t).IsNil(c.SetLabel(POD, "object", "label", "value"), "should have had nil error when setting label") matches, err := c.GetMatches(labels.Everything().Add("label", labels.EqualsOperator, []string{"value"}), POD) Assert(t).IsNil(err, "should have had nil error fetching positive matches") Assert(t).AreEqual(len(matches), 1, "should have had exactly one positive match") matches, err = c.GetMatches(labels.Everything().Add("label", labels.EqualsOperator, []string{"value"}), NODE) Assert(t).IsNil(err, "should have had nil error fetching positive matches for wrong type") Assert(t).AreEqual(len(matches), 0, "should have had exactly zero mistyped matches") matches, err = c.GetMatches(labels.Everything().Add("label", labels.NotInOperator, []string{"value"}), POD) Assert(t).IsNil(err, "should have had nil error fetching negative matches") Assert(t).AreEqual(len(matches), 0, "should have had exactly zero negative matches") }
func getMatches(t *testing.T, httpResponse string) ([]Labeled, error) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { Assert(t).AreEqual(r.URL.Path, endpointSuffix, "Unexpected path requested") Assert(t).AreEqual(r.URL.Query().Get("selector"), "r1=v1,r2=v2", "Unexpected selector requested") Assert(t).AreEqual(r.URL.Query().Get("type"), NODE.String(), "Unexpected type requested") fmt.Fprintln(w, httpResponse) })) defer server.Close() url, err := url.Parse(server.URL + endpointSuffix) Assert(t).IsNil(err, "expected no error parsing url") applicator, err := NewHttpApplicator(nil, url) Assert(t).IsNil(err, "expected no error creating HTTP applicator") selector := labels.Everything().Add("r1", labels.EqualsOperator, []string{"v1"}).Add("r2", labels.EqualsOperator, []string{"v2"}) return applicator.GetMatches(selector, NODE) }
func (rc *replicationController) CurrentNodes() ([]string, error) { selector := klabels.Everything().Add(RCIDLabel, klabels.EqualsOperator, []string{rc.ID().String()}) pods, err := rc.podApplicator.GetMatches(selector, labels.POD) if err != nil { return []string{}, nil } // ID will be something like <nodename>/<podid>. // We just want the nodename. result := make([]string, len(pods)) for i, node := range pods { parts := strings.SplitN(node.ID, "/", 2) // If it so happens that node.ID contains no "/", // then parts[0] is the entire string, so we're OK result[i] = parts[0] } return result, nil }
func TestMatches(t *testing.T) { app := NewFakeApplicator() err := app.SetLabel(NODE, "hi", "foo", "bar") Assert(t).IsNil(err, "expected no error setting label") err = app.SetLabel(NODE, "bye", "you", "bar") Assert(t).IsNil(err, "expected no error setting label") err = app.SetLabel(NODE, "stuff", "foo", "blar") Assert(t).IsNil(err, "expected no error setting label") selector := labels.Everything().Add("foo", labels.EqualsOperator, []string{"bar"}) labeled, err := app.GetMatches(selector, NODE) Assert(t).IsNil(err, "expected no error getting matches") Assert(t).AreEqual(len(labeled), 1, "expected one match") Assert(t).AreEqual(labeled[0].ID, "hi", "expected the ID with the right label to match") }
func scheduledPods(t *testing.T, pods labels.Applicator) []labels.Labeled { podSelector := klabels.Everything().Add("podTest", klabels.EqualsOperator, []string{"successful"}) labeled, err := pods.GetMatches(podSelector, labels.POD) Assert(t).IsNil(err, "expected no error matching pods") return labeled }