func TestJsonPushEventError(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() post("X-GitHub-Event", "push", []byte{}, server.URL+"/build100/secret101/github", http.StatusBadRequest, t) }
func TestJsonPushEvent(t *testing.T) { server := httptest.NewServer(webhook.NewController(&osClient{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() postFile("push", "pushevent.json", server.URL+"/build100/secret101/github", http.StatusOK, t) }
func TestJsonGogsPushEvent(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() postFile("X-Gogs-Event", "push", "pushevent.json", server.URL+"/build100/secret101/github", http.StatusOK, t) }
func TestJsonGitHubPushEventWithCharset(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() postFileWithCharset("X-GitHub-Event", "push", "pushevent.json", server.URL+"/build100/secret101/github", "application/json; charset=utf-8", http.StatusOK, t) }
func TestWrongMethod(t *testing.T) { server := httptest.NewServer(webhook.NewController(&osClient{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() resp, _ := http.Get(server.URL + "/build100/secret101/github") body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), "method") { t.Errorf("Expected BadRequest , got %s: %s!", resp.Status, string(body)) } }
func TestWrongSecret(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() client := &http.Client{} req, _ := http.NewRequest("POST", server.URL+"/build100/wrongsecret/github", nil) resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), webhook.ErrSecretMismatch.Error()) { t.Errorf("Expected BadRequest, got %s: %s!", resp.Status, string(body)) } }
func TestMissingEvent(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() client := &http.Client{} req, _ := http.NewRequest("POST", server.URL+"/build100/secret101/github", nil) req.Header.Add("Content-Type", "application/json") resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), "missing X-GitHub-Event or X-Gogs-Event") { t.Errorf("Expected BadRequest, got %s: %s!", resp.Status, string(body)) } }
func TestWrongGithubEvent(t *testing.T) { server := httptest.NewServer(webhook.NewController(&osClient{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() client := &http.Client{} req, _ := http.NewRequest("POST", server.URL+"/build100/secret101/github", nil) req.Header.Add("Content-Type", "application/json") req.Header.Add("User-Agent", "GitHub-Hookshot/github") req.Header.Add("X-GitHub-Event", "wrong") resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), "Unknown") { t.Errorf("Excepcted BadRequest, got %s: %s!", resp.Status, string(body)) } }
func TestJsonPushEventError(t *testing.T) { server := httptest.NewServer(webhook.NewController(&osClient{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() post("push", []byte{}, server.URL+"/build100/secret101/github", http.StatusBadRequest, t) }
func TestWebhookGithubPush(t *testing.T) { etcdClient := newEtcdClient() m := master.New(&master.Config{ EtcdServers: etcdClient.GetCluster(), }) osMux := http.NewServeMux() storage := map[string]apiserver.RESTStorage{ "builds": buildregistry.NewStorage(build.NewEtcdRegistry(etcdClient)), "buildConfigs": buildconfigregistry.NewStorage(build.NewEtcdRegistry(etcdClient)), } apiserver.NewAPIGroup(m.API_v1beta1()).InstallREST(osMux, "/api/v1beta1") osPrefix := "/osapi/v1beta1" apiserver.NewAPIGroup(storage, runtime.Codec).InstallREST(osMux, osPrefix) apiserver.InstallSupport(osMux) s := httptest.NewServer(osMux) kubeclient := client.NewOrDie(s.URL, nil) osClient, _ := osclient.New(s.URL, nil) whPrefix := osPrefix + "/buildConfigHooks/" osMux.Handle(whPrefix, http.StripPrefix(whPrefix, webhook.NewController(osClient, map[string]webhook.Plugin{ "github": github.New(), }))) info, err := kubeclient.ServerVersion() if err != nil { t.Fatalf("Unexpected error: %v", err) } if e, a := version.Get(), *info; !reflect.DeepEqual(e, a) { t.Errorf("Expected %#v, got %#v", e, a) } // create buildconfig buildConfig := &buildapi.BuildConfig{ JSONBase: kubeapi.JSONBase{ ID: "build100", }, DesiredInput: buildapi.BuildInput{ Type: buildapi.DockerBuildType, SourceURI: "http://my.docker/build", ImageTag: "namespace/builtimage", }, Secret: "secret101", } if _, err := osClient.CreateBuildConfig(buildConfig); err != nil { t.Fatalf("Unexpected error: %v", err) } // trigger build event sending push notification client := &http.Client{} data, err := ioutil.ReadFile("../../pkg/build/webhook/github/fixtures/pushevent.json") if err != nil { t.Fatalf("Failed to open pushevent.json: %v", err) } req, err := http.NewRequest("POST", s.URL+whPrefix+"build100/secret101/github", bytes.NewReader(data)) if err != nil { t.Fatalf("Error creating POST request: %v", err) } req.Header.Add("Content-Type", "application/json") req.Header.Add("User-Agent", "GitHub-Hookshot/github") req.Header.Add("X-Github-Event", "push") resp, err := client.Do(req) if err != nil { t.Fatalf("Failed posting webhook: %v", err) } body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { t.Errorf("Wrong response code, expecting 200, got %s: %s!", resp.Status, string(body)) } // get a list of builds builds, err := osClient.ListBuilds(labels.Everything()) if err != nil { t.Fatalf("Unexpected error: %v", err) } if len(builds.Items) != 1 { t.Fatalf("Expected one build, got %#v", builds) } actual := builds.Items[0] if actual.Status != buildapi.BuildNew { t.Errorf("Expected %s, got %s", buildapi.BuildNew, actual.Status) } if !reflect.DeepEqual(actual.Input, buildConfig.DesiredInput) { t.Errorf("Expected %#v, got %#v", buildConfig.DesiredInput, actual.Input) } }
func (c *config) runApiserver() { minionPort := 10250 osAddr := c.ListenAddr kubePrefix := "/api/v1beta1" osPrefix := "/osapi/v1beta1" kubeClient := c.getKubeClient() osClient := c.getOsClient() etcdClient, etcdServers := c.getEtcdClient() imageRegistry := imageetcd.NewEtcd(etcdClient) // initialize OpenShift API storage := map[string]apiserver.RESTStorage{ "builds": buildregistry.NewStorage(build.NewEtcdRegistry(etcdClient)), "buildConfigs": buildconfigregistry.NewStorage(build.NewEtcdRegistry(etcdClient)), "images": image.NewREST(imageRegistry), "imageRepositories": imagerepository.NewREST(imageRegistry), "imageRepositoryMappings": imagerepositorymapping.NewREST(imageRegistry, imageRegistry), "templateConfigs": template.NewStorage(), } osMux := http.NewServeMux() // initialize webhooks whPrefix := osPrefix + "/buildConfigHooks/" osMux.Handle(whPrefix, http.StripPrefix(whPrefix, webhook.NewController(osClient, map[string]webhook.Plugin{ "github": github.New(), }))) // initialize Kubernetes API podInfoGetter := &kubeclient.HTTPPodInfoGetter{ Client: http.DefaultClient, Port: uint(minionPort), } masterConfig := &master.Config{ Client: kubeClient, EtcdServers: etcdServers, HealthCheckMinions: true, Minions: c.nodeHosts, PodInfoGetter: podInfoGetter, } m := master.New(masterConfig) apiserver.NewAPIGroup(m.API_v1beta1()).InstallREST(osMux, kubePrefix) apiserver.NewAPIGroup(storage, runtime.Codec).InstallREST(osMux, osPrefix) apiserver.InstallSupport(osMux) osApi := &http.Server{ Addr: osAddr, Handler: apiserver.RecoverPanics(osMux), ReadTimeout: 5 * time.Minute, WriteTimeout: 5 * time.Minute, MaxHeaderBytes: 1 << 20, } go util.Forever(func() { glog.Infof("Started Kubernetes API at http://%s%s", osAddr, kubePrefix) glog.Infof("Started OpenShift API at http://%s%s", osAddr, osPrefix) glog.Fatal(osApi.ListenAndServe()) }, 0) }