Skip to content

Commit

Permalink
Added Basic Connection via Curl to Docker Daemon + Ping Test
Browse files Browse the repository at this point in the history
  • Loading branch information
FabiTheGuy committed Sep 19, 2024
1 parent 0947bd6 commit f228c9f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
38 changes: 38 additions & 0 deletions include/docker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef DOCKERXX_DOCKER_HPP
#define DOCKERXX_DOCKER_HPP

#include <curl/curl.h>
#include <string>


namespace dockerxx {

class Docker {

/* Member Variables */
private:
CURL* m_curl = nullptr;
bool m_remote_connection;
std::string m_uri;

/* Constructors */
public:
Docker();
explicit Docker(std::string& uri);

/* Member Functions */
private:
std::string request(const std::string& path);

public:
std::string ping();

/* Static Functions */
private:
static size_t write_data(void* ptr, size_t size, size_t nmemb, std::string* userp);

};

} /* namespace dockerxx */

#endif /* DOCKERXX_DOCKER_HPP */
8 changes: 0 additions & 8 deletions include/dockerxx.hpp

This file was deleted.

61 changes: 58 additions & 3 deletions src/docker.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
//
// Created by fabitheguy on 9/19/24.
//
#include <include/docker.hpp>
#include <stdexcept>
#include <utility>

using namespace dockerxx;

/* Constructors */
Docker::Docker() { this->m_remote_connection = false; }

Docker::Docker(std::string &uri) {
this->m_remote_connection = true;
this->m_uri = std::move(uri);
}

/* Member functions */
std::string Docker::request(const std::string &path) {
CURLcode response_code;
std::string response_data;

if (path.empty()) {
throw std::invalid_argument("Path is empty");
}

this->m_curl = curl_easy_init();

if (this->m_remote_connection) {
curl_easy_setopt(this->m_curl, CURLOPT_URL, std::string(this->m_uri + path).c_str());
} else {
curl_easy_setopt(this->m_curl, CURLOPT_URL, std::string("http://localhost" + path).c_str());
curl_easy_setopt(this->m_curl, CURLOPT_UNIX_SOCKET_PATH,
std::string("/var/run/docker.sock").c_str());
}

curl_easy_setopt(this->m_curl, CURLOPT_WRITEFUNCTION, Docker::write_data);
curl_easy_setopt(this->m_curl, CURLOPT_WRITEDATA, &response_data);

response_code = curl_easy_perform(this->m_curl);

if (response_code != CURLE_OK) {
throw std::runtime_error("curl_easy_perform() failed: " +
std::string(curl_easy_strerror(response_code)));
}

curl_easy_cleanup(this->m_curl);

return response_data;
}

std::string Docker::ping() { return request("/_ping"); }

/* Static functions */
size_t Docker::write_data(void *ptr, size_t size, size_t nmemb, std::string *userp) {
size_t real_size = size * nmemb;

userp->append(static_cast<char *>(ptr), real_size);

return real_size;
}
7 changes: 7 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include <docker.hpp>
#include <gtest/gtest.h>

TEST(DockerConnection, PingDaemon) {
dockerxx::Docker docker;

ASSERT_EQ(docker.ping(), "OK");
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);

Expand Down

0 comments on commit f228c9f

Please sign in to comment.