Skip to content

Commit

Permalink
Make SotoTestUtils a library (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Oct 12, 2020
1 parent 35af9ec commit be173c7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ jobs:
fetch-depth: 1
- name: Xcodebuild
run: |
xcodebuild -scheme soto-core -quiet -destination 'platform=iOS Simulator,name=iPhone 11'
xcodebuild test -scheme soto-core -destination 'platform=iOS Simulator,name=iPhone 11'
xcodebuild -scheme soto-core-Package -quiet -destination 'platform=iOS Simulator,name=iPhone 11'
xcodebuild test -scheme soto-core-Package -destination 'platform=iOS Simulator,name=iPhone 11'
linux:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let package = Package(
name: "soto-core",
products: [
.library(name: "SotoCore", targets: ["SotoCore"]),
.library(name: "SotoTestUtils", targets: ["SotoTestUtils"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
Expand Down
28 changes: 28 additions & 0 deletions Sources/SotoTestUtils/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Soto for AWS open source project
//
// Copyright (c) 2017-2020 the Soto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Soto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if os(Linux)
import Glibc
#else
import Darwin.C
#endif

internal enum Environment {
internal static subscript(_ name: String) -> String? {
guard let value = getenv(name) else {
return nil
}
return String(cString: value)
}
}
15 changes: 10 additions & 5 deletions Sources/SotoTestUtils/TestServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import NIO
import NIOFoundationCompat
import NIOHTTP1
import NIOTestUtils
@testable import SotoCore
import SotoCore
import SotoXML
import XCTest

Expand Down Expand Up @@ -220,6 +220,11 @@ extension AWSTestServer {

/// fake ec2 metadata server
public func ec2MetadataServer(version: IMDSVersion, metaData: EC2InstanceMetaData? = nil) throws {
struct InstanceMetaData {
static let CredentialUri = "/latest/meta-data/iam/security-credentials/"
static let TokenUri = "/latest/api/token"
static let TokenHeaderName = "X-aws-ec2-metadata-token"
}
let ec2Role = "ec2-testserver-role"
let token = "345-34hj-345jk-4875"
let metaData = metaData ?? EC2InstanceMetaData.default
Expand All @@ -230,7 +235,7 @@ extension AWSTestServer {

try self.processRaw { request in
switch (request.method, request.uri) {
case (.PUT, InstanceMetaDataClient.TokenUri):
case (.PUT, InstanceMetaData.TokenUri):
// Token access
switch version {
case .v1:
Expand All @@ -242,17 +247,17 @@ extension AWSTestServer {
return .result(.init(httpStatus: .ok, headers: headers, body: responseBody), continueProcessing: true)
}

case (.GET, InstanceMetaDataClient.CredentialUri):
case (.GET, InstanceMetaData.CredentialUri):
// Role name
guard version == .v1 || request.headers[InstanceMetaDataClient.TokenHeaderName] == token else {
guard version == .v1 || request.headers[InstanceMetaData.TokenHeaderName] == token else {
return .error(.badRequest, continueProcessing: false)
}
var responseBody = byteBufferAllocator.buffer(capacity: ec2Role.utf8.count)
responseBody.writeString(ec2Role)
let headers: [String: String] = [:]
return .result(.init(httpStatus: .ok, headers: headers, body: responseBody), continueProcessing: true)

case (.GET, InstanceMetaDataClient.CredentialUri + ec2Role):
case (.GET, InstanceMetaData.CredentialUri + ec2Role):
// credentials
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .formatted(dateFormatter)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SotoTestUtils/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import Foundation
import Logging
@testable import SotoCore
import SotoCore

@propertyWrapper public struct EnvironmentVariable<Value: LosslessStringConvertible> {
var defaultValue: Value
Expand Down

0 comments on commit be173c7

Please sign in to comment.