Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

first unit test #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions arbiter/pkg/arbiter/controllerinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ under the License.
package arbiter

import (
"fmt"
"log"
"strings"

Expand Down Expand Up @@ -85,37 +86,47 @@ func (arb *Arbiter) ListenForControllers() {

}

func (arb *Arbiter) scanAbort(w http.ResponseWriter, r *http.Request) {

log.Println("Request scanAbort")
params := mux.Vars(r)
func scanAbortLogic(id string, imageHash string, cds map[string]*controllerDaemon, images map[string]*assignImage, assignedImageCount int) (statusCode int, logMessage string, cd *controllerDaemon, image *assignImage, err *jsonErr) {
var ok bool
cd, ok = cds[id]

imageHash := params["id"]

var ci controllerInfo
_ = json.NewDecoder(r.Body).Decode(&ci)

cd, ok := arb.controllerDaemons[ci.Id]
if !ok {
log.Printf("Unknown controller [%s] claimed abort for image: %s\n", ci.Id, imageHash)
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"})
logMessage = fmt.Sprintf("Unknown controller [%s] claimed abort for image: %s\n", id, imageHash)
statusCode = http.StatusNotFound
err = &jsonErr{Code: http.StatusNotFound, Text: "Not Found"}
return
}

image, ok := arb.assignedImages[imageHash]
image, ok = images[imageHash]
if !ok {
log.Printf("Controller [%s] claimed abort on unknown image: %s\n", ci.Id, imageHash)
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"})
logMessage = fmt.Sprintf("Controller [%s] claimed abort on unknown image: %s\n", id, imageHash)
statusCode = http.StatusNotFound
err = &jsonErr{Code: http.StatusNotFound, Text: "Not Found"}
return
}

arb.releaseScanForPeer(image, cd, imageHash)
statusCode = http.StatusOK
logMessage = fmt.Sprintf("Done abortScan - Hub jobs: %d", assignedImageCount)
return
}

w.WriteHeader(http.StatusOK)
log.Printf("Done abortScan - Hub jobs: %d", arb.assignedImageCount())
func (arb *Arbiter) scanAbort(w http.ResponseWriter, r *http.Request) {
log.Println("Request scanAbort")
params := mux.Vars(r)
imageHash := params["id"]

var ci controllerInfo
_ = json.NewDecoder(r.Body).Decode(&ci)

statusCode, logMessage, cd, image, err := scanAbortLogic(ci.Id, imageHash, arb.controllerDaemons, arb.assignedImages, arb.assignedImageCount())

if err == nil {
arb.releaseScanForPeer(image, cd, imageHash)
} else {
json.NewEncoder(w).Encode(err)
}
w.WriteHeader(statusCode)
log.Printf(logMessage)
}

func (arb *Arbiter) releaseScanForPeer(image *assignImage, cd *controllerDaemon, imageHash string) {
Expand Down
20 changes: 20 additions & 0 deletions arbiter/pkg/arbiter/controllerinterface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package arbiter

import (
"net/http"
"testing"
)

func TestScanAbortLogic(t *testing.T) {
statusCode, logMessage, _, _, err := scanAbortLogic("abc", "123", make(map[string]*controllerDaemon), make(map[string]*assignImage), 0)
if statusCode != http.StatusNotFound {
t.Error("wrong status code, expected StatusNotFound")
}
expectedMessage := "Unknown controller [abc] claimed abort for image: 123\n"
if logMessage != expectedMessage {
t.Error("wrong log message, got", logMessage, "instead of", expectedMessage)
}
if err == nil {
t.Error("expected error, got nil")
}
}