Skip to content

Commit

Permalink
refactor(core): Use a rest controller for status endpoints (#388)
Browse files Browse the repository at this point in the history
* test(core): Update tests to test statusHandler

The status endpoint tests are currently testing AllStatusEndpoint and
InstanceStatusEndpoint which are pass-through classes that will be
removed; update the tests to directly test StatusHandler.

* refactor(core): Use a rest controller for status endpoints

The Endpoint classes that are used to implement the status endpoint
have significantly changed in Spring Boot 2. Instead of creating and
exposing an endpoint, just use a rest controller.

* fix(core): Remove package from componentscan

The prior commit moved everything out of the endpoints package into
other packages, so we should remove it from the component scan.
  • Loading branch information
ezimanyi committed Jun 24, 2019
1 parent 276152c commit e04da7d
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import javax.servlet.Filter
@ComponentScan([
"com.netflix.spinnaker.rosco.config",
"com.netflix.spinnaker.rosco.controllers",
"com.netflix.spinnaker.rosco.endpoints",
"com.netflix.spinnaker.rosco.executor",
"com.netflix.spinnaker.rosco.filters",
"com.netflix.spinnaker.rosco.jobs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2016 Schibsted ASA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
* 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,
Expand All @@ -14,15 +14,19 @@
* limitations under the License.
*/

package com.netflix.spinnaker.rosco.endpoints
package com.netflix.spinnaker.rosco.controllers

import com.netflix.spinnaker.rosco.api.BakeStatus
import com.netflix.spinnaker.rosco.persistence.BakeStore
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Component
class StatusHandler {
@ConditionalOnProperty(value = "endpoints.status.enabled", matchIfMissing = true)
@RestController
@RequestMapping("/status")
class StatusController {

private final BakeStore bakeStore
private final String roscoInstanceId
Expand All @@ -32,16 +36,18 @@ class StatusHandler {
}

@Autowired
StatusHandler(BakeStore bakeStore, String roscoInstanceId) {
StatusController(BakeStore bakeStore, String roscoInstanceId) {
this.bakeStore = bakeStore
this.roscoInstanceId = roscoInstanceId
}

@RequestMapping("/instance")
public Map<String, Object> instanceIncompleteBakes() {
def instanceIncompleteBakeIds = bakeStore.getThisInstanceIncompleteBakeIds()
return getBakesAndInstanceStatus(instanceIncompleteBakeIds)
}

@RequestMapping("/all")
public Map<String, Object> allIncompleteBakes() {
def instances = [:]
def allIncompleteBakeIds = bakeStore.getAllIncompleteBakeIds()
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2016 Schibsted ASA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
* 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,
Expand All @@ -14,10 +14,11 @@
* limitations under the License.
*/

package com.netflix.spinnaker.rosco.endpoints
package com.netflix.spinnaker.rosco.controllers

import com.google.common.collect.Sets
import com.netflix.spinnaker.rosco.api.BakeStatus
import com.netflix.spinnaker.rosco.controllers.StatusController
import com.netflix.spinnaker.rosco.persistence.RedisBackedBakeStore
import spock.lang.Specification
import spock.lang.Subject
Expand All @@ -34,13 +35,12 @@ class AllStatusEndpointSpec extends Specification {
void 'all instances incomplete bakes with status'() {
setup:
def bakeStoreMock = Mock(RedisBackedBakeStore)
def statusHandler = new StatusHandler(bakeStoreMock, localInstanceId)

@Subject
def statusEndpoint = new AllStatusEndpoint(statusHandler)
def statusHandler = new StatusController(bakeStoreMock, localInstanceId)

when:
def instances = statusEndpoint.invoke()
def instances = statusHandler.allIncompleteBakes()
then:
1 * bakeStoreMock.getAllIncompleteBakeIds() >> [(localInstanceId): Sets.newHashSet(LOCAL_JOB_ID), (remoteInstanceId): Sets.newHashSet(REMOTE_JOB_ID)]
1 * bakeStoreMock.retrieveBakeStatusById(LOCAL_JOB_ID) >> localRunningBakeStatus
Expand All @@ -52,13 +52,10 @@ class AllStatusEndpointSpec extends Specification {
setup:
def bakeStoreMock = Mock(RedisBackedBakeStore)
bakeStoreMock.getAllIncompleteBakeIds() >> new HashMap<String, Set<String>>()
def statusHandler = new StatusHandler(bakeStoreMock, localInstanceId)

@Subject
def statusEndpoint = new AllStatusEndpoint(statusHandler)
def statusHandler = new StatusController(bakeStoreMock, localInstanceId)

when:
def instances = statusEndpoint.invoke()
def instances = statusHandler.allIncompleteBakes()

then:
1 * bakeStoreMock.getAllIncompleteBakeIds()
Expand All @@ -68,13 +65,10 @@ class AllStatusEndpointSpec extends Specification {
void 'no instances incomplete bakes'() {
setup:
def bakeStoreMock = Mock(RedisBackedBakeStore)
def statusHandler = new StatusHandler(bakeStoreMock, localInstanceId)

@Subject
def statusEndpoint = new AllStatusEndpoint(statusHandler)
def statusHandler = new StatusController(bakeStoreMock, localInstanceId)

when:
def instances = statusEndpoint.invoke()
def instances = statusHandler.allIncompleteBakes()

then:
instances == [instance: localInstanceId, instances: [:]]
Expand All @@ -84,13 +78,10 @@ class AllStatusEndpointSpec extends Specification {
setup:
def bakeStoreMock = Mock(RedisBackedBakeStore)
bakeStoreMock.getAllIncompleteBakeIds() >> { throw new RuntimeException() }
def statusHandler = new StatusHandler(bakeStoreMock, localInstanceId)

@Subject
def statusEndpoint = new AllStatusEndpoint(statusHandler)
def statusHandler = new StatusController(bakeStoreMock, localInstanceId)

when:
statusEndpoint.invoke()
statusHandler.allIncompleteBakes()

then:
thrown(RuntimeException)
Expand All @@ -102,13 +93,10 @@ class AllStatusEndpointSpec extends Specification {
bakeStoreMock.getAllIncompleteBakeIds() >> [(localInstanceId): Sets.newHashSet(LOCAL_JOB_ID), (remoteInstanceId): Sets.newHashSet(REMOTE_JOB_ID)]
bakeStoreMock.retrieveBakeStatusById(LOCAL_JOB_ID) >> { throw new RuntimeException() }
bakeStoreMock.retrieveBakeStatusById(REMOTE_JOB_ID) >> { throw new RuntimeException() }
def statusHandler = new StatusHandler(bakeStoreMock, localInstanceId)

@Subject
def statusEndpoint = new AllStatusEndpoint(statusHandler)
def statusHandler = new StatusController(bakeStoreMock, localInstanceId)

when:
statusEndpoint.invoke()
statusHandler.allIncompleteBakes()

then:
thrown(RuntimeException)
Expand Down
Loading

0 comments on commit e04da7d

Please sign in to comment.