-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Trace.swift | ||
// Buildkite | ||
// | ||
// Created by Aaron Sky on 6/5/22. | ||
// Copyright © 2022 Aaron Sky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public struct Trace: Codable, Equatable, Identifiable { | ||
/// a unique identifier for this test result | ||
public var id: UUID | ||
/// a group or topic for the test | ||
/// | ||
/// - `Student.isEnrolled()` | ||
public var scope: String? | ||
/// a name or description for the test | ||
/// | ||
/// - `Manager.isEnrolled()` | ||
/// - `returns_boolean` | ||
public var name: String? | ||
/// a unique identifier for the test. If your test runner supports it, use the identifier needed to rerun this test | ||
/// | ||
/// - `Manager.isEnrolled#returns_boolean` | ||
public var identifier: String? | ||
/// the file and line number where the test originates, separated by a colon (:) | ||
/// | ||
/// - `./tests/Manager/isEnrolled.js:32` | ||
public var location: String? | ||
/// the file where the test originates | ||
/// | ||
/// - `./tests/Manager/isEnrolled.js` | ||
public var fileName: String? | ||
/// the outcome of the test, e.g. `"passed"`, `"failed"`, `"skipped"`, or `"unknown"`` | ||
public var result: String? | ||
/// if applicable, a short summary of why the test failed | ||
/// | ||
/// - `Expected Boolean, got Object` | ||
public var failureReason: String? | ||
/// History object | ||
public var history: Span | ||
|
||
public struct Span: Codable, Equatable { | ||
/// A section category for this span, e.g. `"http"`, `"sql"`, `"sleep"`, or `"annotation"` | ||
public var section: String | ||
/// A monotonically increasing number | ||
public var startAt: TimeInterval? | ||
/// A monotonically increasing number | ||
public var endAt: TimeInterval? | ||
/// How long the span took to run | ||
public var duration: TimeInterval? | ||
/// Any information related to this span | ||
public var detail: [String: String] = [:] | ||
/// array of span objects | ||
public var children: [Span] = [] | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
Sources/Buildkite/Resources/TestAnalytics/TestAnalyticsUpload.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// TestAnalyticsUpload.swift | ||
// Buildkite | ||
// | ||
// Created by Aaron Sky on 6/5/22. | ||
// Copyright © 2022 Aaron Sky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public enum TestAnalytics { | ||
/// Resources for requesting information about your organization's Buildkite agents. | ||
public enum Resources {} | ||
} | ||
|
||
extension TestAnalytics.Resources { | ||
/// Get metrics about agents active with the current organization. | ||
public struct Upload: Resource { | ||
public struct Body: Encodable { | ||
public var format: String | ||
public var environment: Environment | ||
public var data: [Trace] | ||
|
||
public struct Environment: Encodable { | ||
public var ci: String | ||
public var key: String | ||
public var number: String? | ||
public var jobId: String? | ||
public var branch: String? | ||
public var commitSha: String? | ||
public var message: String? | ||
public var url: String? | ||
} | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case format | ||
case environment = "runEnv" | ||
case data | ||
} | ||
} | ||
|
||
public struct Content: Codable, Equatable { | ||
public var id: UUID | ||
public var runId: UUID | ||
public var queued: Int | ||
public var skipped: Int | ||
public var errors: [String] | ||
public var runUrl: URL | ||
} | ||
|
||
public var version: APIVersion { | ||
APIVersion.TestAnalytics.v1 | ||
} | ||
|
||
public let path = "upload" | ||
|
||
/// body of the request | ||
public var body: Body | ||
|
||
public init( | ||
body: Body | ||
) { | ||
self.body = body | ||
} | ||
} | ||
} | ||
|
||
extension Resource where Self == TestAnalytics.Resources.Upload { | ||
/// Get an object with properties describing Buildkite | ||
/// | ||
/// Returns meta information about Buildkite. | ||
public static func uploadTestAnalytics(_ data: [Trace], environment: Self.Body.Environment) -> Self { | ||
Self(body: .init(format: "json", environment: environment, data: data)) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Tests/BuildkiteTests/Resources/TestAnalytics/TestAnalyticsUploadTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// TestAnalyticsUploadTests.swift | ||
// Buildkite | ||
// | ||
// Created by Aaron Sky on 6/6/22. | ||
// Copyright © 2022 Aaron Sky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import Buildkite | ||
|
||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
class TestAnalyticsUpload: XCTestCase { | ||
func testTestAnalyticsUpload() async throws { | ||
typealias Resource = TestAnalytics.Resources.Upload | ||
|
||
let traces: [Trace] = [ | ||
.init(id: .init(), history: .init(section: "http")), | ||
.init(id: .init(), history: .init(section: "http")), | ||
.init(id: .init(), history: .init(section: "http")), | ||
] | ||
let expected = Resource.Content(id: .init(), runId: .init(), queued: 0, skipped: 0, errors: [], runUrl: .init()) | ||
let context = try MockContext(content: expected) | ||
|
||
let response = try await context.client.send( | ||
.uploadTestAnalytics(traces, environment: .init(ci: "buildkite", key: "test")) | ||
) | ||
|
||
XCTAssertEqual(expected, response.content) | ||
} | ||
} |